[Python-Dev] PEP 318: Properties

Bob Ippolito bob at redivi.com
Sun Apr 4 21:22:09 EDT 2004


On Apr 4, 2004, at 9:07 PM, Jewett, Jim J wrote:

>     Skip> x = 42
>
>     Skip> def x(self) [propget]:
>     Skip>   "Doc string for x"
>     Skip>   return self.__x
>
>     Guido> It's broken.  I expect this to raise an exception at some 
> point.
>     Guido> Beyond that, who cares?
>
>     Jim> If so, then this decorator is a bad idea.
>
>     Jim> I would expect 42 to be the initial value of the property x.
>
>     Skip> Then it makes sense to put "x = 42" after the three property
>     Skip> definitions (before the end of the class definition).
>     Skip> Would that work?
>
> Not really.  The documentation says
>
> 	"Properties are a neat way to implement attributes whose usage
> 	resembles attribute access, but whose implementation uses method
> 	calls."
>
> This suggests that creating a property creates a manager.  In this 
> case,
> the manager is created for an attribute that already exists.  If it 
> uses
> the previous value as the initial current value, that is what I expect.
> If it raises an exception ("I can't manage that!  Someone beat me to 
> it!"),
> I'll be annoyed, but live with the limitation.  If it silently throws 
> the
> value away, or raises an exception at run time -- that is bad.
>
> Yes, this is a variation on the "Properties do not work for classic
> classes, but you don't get a clear error when you try this." wart.
> But the code this decorator replaces::
>
>     x = property(getx, setx)
>
> does explicitly replace x, so I think *this* confusion may be specific
> to the decorator.

... have you ever used descriptors before?

Property descriptors are only functionally applicable to *instances* of 
the class where the descriptor lives.  FooClass.x = 42 will replace the 
descriptor.  It's possible to make a descriptor do something special 
when you fetch it from its class, but in this case FooClass doesn't 
even really exist when you are replacing the descriptor with something 
else!

-bob




More information about the Python-Dev mailing list