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

Guido van Rossum guido at python.org
Tue Oct 18 06:59:18 CEST 2005


On 10/17/05, Steven Bethard <steven.bethard at gmail.com> wrote:
> 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.

That is probably as good as you can get it *if* you prefer the nested
class over a property call with string arguments. Personally, I find
the nested class inheriting from Property a lot more "magical" than
the call to property() with string arguments.

I wonder if at some point in the future Python will have to develop a
macro syntax so that you can write

    Property foo:
        def get(self): return self._foo
        ...etc...

which would somehow translate into code similar to your recipe.

But until then, I prefer the simplicity of

    foo = property('get_foo', 'set_foo')

--
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list