[Python-Dev] Re: Python-Dev Digest, Vol 9, Issue 16

Edward Loper edloper at gradient.cis.upenn.edu
Sun Apr 4 20:33:57 EDT 2004


>>>How would this be interpreted?
>>>    x = 42
>>>    def x(self) [propget]:
>>>        "Doc string for x"
>>>        return self.__x
> 
>>>That is, there is already an (otherwise invalid) 'x' in the calling scope
>>>when propget() is called.  Do the property doodads just need to be
>>>bulletproofed or should an exception be raised?

[GvR said:]
>>It's broken.  I expect this to raise an exception at some point.
>>Beyond that, who cares?

[Jim said:]
> If so, then this decorator is a bad idea.
> 
> I would expect 42 to be the initial value of the property x.

That doesn't make sense.  If you want to set a default value, you'd say:

     __x = 42

A property's getter/setter/deleter can perform arbitrary actions, and in 
the general case there's no sensible way to define an "initial value" 
for a property.  E.g., consider:

     class A:
         def getX(self):
             return random.choice(['apple', 'orange', 'grape'])
         x = property(getX)

If this still doesn't make sense, go read the docs for properties:
<http://www.python.org/2.2.2/descrintro.html#property>

[Skip said:]
> Then it makes sense to put "x = 42" after the three property definitions
> (before the end of the class definition).  Would that work?

No, that would just overwrite x with the value 42 (throwing away the 
property object).

-Edward




More information about the Python-Dev mailing list