[Python-Dev] Definining properties - a use case for class decorators?

Steven Bethard steven.bethard at gmail.com
Tue Oct 18 06:46:12 CEST 2005


Barry Warsaw wrote:
> On Mon, 2005-10-17 at 21:55, Guido van Rossum wrote:
>
> > Let's change the property built-in so that its arguments can be either
> > functions or strings (or None). If they are functions or None, it
> > behaves exactly like it always has.
> >
> > If an argument is a string, it should be a method name, and the method
> > is looked up by that name each time the property is used. Because this
> > is late binding, it can be put before the method definitions, and a
> > subclass can override the methods. Example:
> >
> > class C:
> >
> >     foo = property('getFoo', 'setFoo', None, 'the foo property')
> >
> >     def getFoo(self):
> >         return self._foo
> >
> >     def setFoo(self, foo):
> >         self._foo = foo
> >
> > What do you think?
>
> Ick, for all the reasons that strings are less appealing than names.

I'm not sure if you'll like it any better, but I combined Michael
Urman's suggestion with my late-binding property recipe to get:
    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/442418
It solves the name-repetition problem and the late-binding problem (I
believe), at the cost of either adding an extra argument to the
functions forming the property or confusing the "self" argument a
little.

STeVe
--
You can wordify anything if you just verb it.
        --- Bucky Katt, Get Fuzzy


More information about the Python-Dev mailing list