This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: property example code error
Type: Stage:
Components: Documentation Versions: Python 2.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: georg.brandl, georg.brandl, jafo, ojokimu
Priority: normal Keywords:

Created on 2005-04-01 20:09 by ojokimu, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg24851 - (view) Author: John Ridley (ojokimu) Date: 2005-04-01 20:09
The example code for 'property' in lib/built-in-funcs.html may 
produce an error if run "as is": 
 
Python 2.4.1 (#1, Mar 31 2005, 21:33:58) 
[GCC 3.4.1 (Mandrakelinux (Alpha 3.4.1-3mdk)] on linux2 
>>> class C(object): 
...     def getx(self): return self.__x 
...     def setx(self, value): self.__x = value 
...     def delx(self): del self.__x 
...     x = property(getx, setx, delx, "I'm the 'x' property.") 
... 
>>> c=C() 
>>> c.x 
Traceback (most recent call last): 
  File "<stdin>", line 1, in ? 
  File "<stdin>", line 2, in getx 
AttributeError: 'C' object has no attribute '_C__x' 
 
The same goes for 'del c.x' (although not 'c.x = 0', of course). 
 
A more "typical" way of defining managed attributes would be to 
include an 'init' as follows: 
 
class C(object): 
    def __init__(self): self.__x = None 
    def getx(self): return self.__x 
    def setx(self, value): self.__x = value 
    def delx(self): del self.__x 
    x = property(getx, setx, delx, "I'm the 'x' property.") 
 
 
msg24852 - (view) Author: Sean Reifschneider (jafo) * (Python committer) Date: 2005-04-06 03:33
Logged In: YES 
user_id=81797

I agree, adding the __init__ to set a value would be useful.  +1
msg24853 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2005-06-25 20:08
Logged In: YES 
user_id=1188172

Thanks for the report; fixed as Doc/lib/libfuncs.tex r1.184,
r1.175.2.4.
History
Date User Action Args
2022-04-11 14:56:10adminsetgithub: 41789
2005-04-01 20:09:35ojokimucreate