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

Josiah Carlson jcarlson at uci.edu
Wed Oct 19 22:10:30 CEST 2005


Jim Jewett <jimjjewett at gmail.com> wrote:
> (In http://mail.python.org/pipermail/python-dev/2005-October/057409.html,)
> Nick Coghlan suggested allowing attribute references as binding targets.
> 
> >    x = property("Property x (must be less than 5)")
> 
> >    def x.get(instance):  ...
> 
> Josiah shivered and said it was hard to tell what was even intended, and
> (in http://mail.python.org/pipermail/python-dev/2005-October/057437.html)
> Nick agreed that it was worse than
> 
> >    x.get = f given:
> >        def f(): ...
> 
> Could someone explain to me why it is worse?

    def x.get(...): ...

Seems to imply that one is defining a method on x.  This is not the case.
It is also confused by the x.get(instance) terminology that I doubt has
ever seen light of day in production code.  Instance of what?  Instance
of x?  The class?  ...

I'm personally averse to the 'given:' syntax, if only because under
certain situations, it can be reasonably emulated.


> I understand not wanting to modify object x outside of its definition.
> 
> I understand that there is some trickiness about instancemethods
> and bound variables.
> 
> But these objections seem equally strong for both forms, as well
> as for the current "equivalent" of
> 
>     def f(): ...
>     x.get = f
> 
> The first form (def x.get) at least avoids repeating (or even creating)
> the temporary function name.
> 
> The justification for decorators was to solve this very problem within
> a module or class.  How is this different?  Is it just that attributes
> shouldn't be functions, and this might encourage the practice?

Many will agree that there is a problem with how properties are defined.
There are many proposed solutions, some of which use decorators, custom
subclasses, metaclasses, etc.

I have a problem with it because from the description, you could use...

    def x.y.z.a.b.c.foobarbaz(...):
        ...

...and it woud be unclear to the reader or writer what the hell
x.y.z.a.b.c is (class, instance, module), which can come up if the
definition/import of x is far enough away from the definition of
x.<blah> .  Again, ick.

 - Josiah



More information about the Python-Dev mailing list