Using property in classic class may not work
Alex Martelli
aleax at aleax.it
Tue Apr 15 04:18:22 EDT 2003
Neil Hodgson wrote:
> The "property" feature in Python 2.2 appears very helpful to me and
> seems to work on both classic and new-style classes. However, pychecker
> gives this error message:
>
> DemonTerm.py:563: Using property (frame) in classic class DemonFrame may
> not work
>
> The class in question is derived from a wxWindows class, so I can't
> make it derive just from object. The "What's New in Python 2.2" document
> only mentions property wrt new-style clases. Can I depend on property
> working for classic classes? Is there a workaround such as deriving from
> object as well?
No, and yes, respectively -- deriving from object means your class
isn't classic any more, so properties do work as intended there. Vide:
>>> class X:
... def __init__(self, x=23): self.xx=x
... def getx(self): return 1000+self.xx
... def setx(self,x): self.xx=x
... x=property(getx,setx)
...
>>> x=X()
>>> x.x
1023
>>> x.x=99
>>> x.x
99
The assignment hasn't been caught by setx, so x.xx is still
23 and x.x is now the integer 99, not a property any more. But:
>>> class Xob(X,object): pass
...
>>> x=Xob()
>>> x.x
1023
>>> x.x=99
>>> x.x
1099
>>>
as you see, this does work as intended.
Alex
More information about the Python-list
mailing list