I've been doing some python programming lately, and I realized if I could somehow avoid writing individual getters and setters for every single member variable. Lo and behold, properties. I stumble onto this guy's page and he confirms: mutator functions are l4m3z0rz, and python allows you to skip them [b]with properties![/b] I am ecstatic. Then I find out, you basically have to write getters and setters for each individual member and then wrap them in a call to property().
What is the deal?
Here's the page:
http://tomayko.com/writings/getters-setters-fuxors
Please, dear god, someone enlighten me.
and I realized it would be cool if I could somehow avoid writing individual getters and setters for every single member variable
fuck my 2am mind
Why, lambdas, of course. Syntactic sugar for getters/setters, but better than individual methods.
You don't need getters and setters in Python. You can modify variables directly.
This is why I think properties are useful:
When you write code, you should keep things as simple as possible, so you shouldn't write getters and setters in Python. But what if you later need to change the behaviour of that class without changing its external interface?
Let's say, for example, that you change your Point class so that it supports persistence. Originally you were assigning directly to x and y, but now you'd also like to track when x or y have changed without altering code outside that class. Properties come to the rescue; it still behaves like assignment, but you'd doing something else underneath.
In practice I haven't found much use for them.
properties are great for doing really complicated things with a very simple-looking interface. iirc sqlalchemy uses them quite a bit.