[Tutor] Class Construction question [C++ and Python class
introduction]
Magnus Lycka
magnus@thinkware.se
Wed Nov 13 09:28:08 2002
At 14:38 2002-11-13 +0100, Yigal Duppen wrote:
>Even better, (something I realised only recently), as of Python2.2 you can
>also change a normal attribute to a property! No more fidgeting with the
>(quite dangerous IMHO) __getattr__ and __setattr__.
Right, I forgot about new style classes.
>class C(object):
>
> def __init__(self):
> self.a = 10
>
> def setA(self, a):
> self._a = a
>
> def getA(self):
> return self._a
>
> a = property(getA, setA, None, "a is now a property!!")
This is obviously much cleaner if you have several attribute
that you want to wrap up, since you don't have to mix all get
methods in the class in one __getattr__ and all set methods in
one __setattr__.
This is something to use if you don't need to support python
versions older than 2.2 and don't run into special problems.
For instance I realized I couldn't inherit from both new style
classes and ZODB's Persistent base class. I decided to leave
new style classes alone until 2.3, hoping that some issues will
be solved by then.
--
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/ mailto:magnus@thinkware.se