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

Fredrik Lundh fredrik at pythonware.com
Wed Oct 19 22:23:35 CEST 2005


Guido van Rossum wrote:

> OK, so how's this for a radical proposal.
>
> 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?

+1 from here.

> If you can think of a solution that looks better than mine, you're a genius.

letting "class" inject a slightly magic "self" variable into the class namespace ?

    class C:

        foo = property(self.getFoo, self.setFoo, None, 'the foo property')

        def getFoo(self):
            return self._foo

        def setFoo(self, foo):
            self._foo = foo

(figuring out exactly what "self" should be is left as an exercise etc)

</F>





More information about the Python-Dev mailing list