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.")
|