[Python-Dev] Declaring setters with getters (original) (raw)

glyph at divmod.com glyph at divmod.com
Thu Nov 1 07:58:52 CET 2007


As long as we're all tossing out ideas here, my 2ยข. I vastly prefer this:

On 02:43 am, steven.bethard at gmail.com wrote:

On 10/31/07, Fred Drake <fdrake at acm.org> wrote:

@property.set def attribute(self, value): self.ignored = value

to this:

@property.set(attribute) def attribute(self, value): self.ignored = value

since I don't see any additional expressive value in the latter, and it provides an opportunity to make a mistake. The decorator syntax's main value, to me, is eliminating the redundancy in:

def foo():
    ...
foo = bar(foo)

eliminating the possibility of misspelling "foo" one of those three times. and removing a lot of finger typing.

The original proposal here re-introduces half of this redundancy. I think I can see why Guido did it that way - it makes the implementation a bit more obvious - but decorators are already sufficiently "magic" that I wouldn't mind a bit more to provide more convenience, in what is apparently just a convenience mechanism.

And, since everyone else is sharing their personal current way of idiomatically declaring dynamic properties, here's mine; abusing the "class" statement instead of decorators:

from epsilon.descriptor import attribute
class Stuff(object):
    class foo(attribute):
        "you can put a docstring in the obvious place"
        def set(self, value):
            print 'set foo!'
            self._foo = value + 4
        def get(self):
            return self._foo + 3

s = Stuff()
s.foo = 0
print 's.foo:', s.foo

I'd be glad of a standard, accepted way to do this though, since it's really just a spelling issue and it would be nice to reduce the learning curve between all the different libraries which define dynamic attributes.



More information about the Python-Dev mailing list