python properties (5)

1 Name: #!/usr/bin/anonymous : 2009-07-31 07:19 ID:4kqye6U0

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.

2 Name: #!/usr/bin/anonymous : 2009-07-31 07:34 ID:4kqye6U0

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

3 Name: #!/usr/bin/anonymous : 2009-07-31 09:04 ID:4kqye6U0

Why, lambdas, of course. Syntactic sugar for getters/setters, but better than individual methods.

4 Name: dmpk2k!hinhT6kz2E : 2009-07-31 13:52 ID:Heaven

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.

5 Name: #!/usr/bin/anonymous : 2009-08-01 02:59 ID:Heaven

properties are great for doing really complicated things with a very simple-looking interface. iirc sqlalchemy uses them quite a bit.

This thread has been closed. You cannot post in this thread any longer.