change property after inheritance
Maric Michaud
maric at aristote.info
Wed Sep 6 11:44:42 EDT 2006
Le mercredi 06 septembre 2006 16:33, David Isaac a écrit :
> Suppose a class has properties and I want to change the
> setter in a derived class. If the base class is mine, I can do this:
> http://www.kylev.com/2004/10/13/fun-with-python-properties/
> Should I? (I.e., is that a good solution?)
Why not ? This ontroduce the notion of public getter a la C++/Java while the
property is overloadable by itself (as below), but it's correct design IMHO.
> And what if I cannot change the base class?
> How to proceed then?
Like that :
In [44]: class a(object) :
....: p=property(lambda s : getattr(s, '_p', None))
....:
....:
In [46]: class b(a) :
....: p=property(a.p.fget, lambda s, v : setattr(s, '_p', v))
....:
....:
In [47]: print a().p
None
In [48]: a().p = 5
---------------------------------------------------------------------------
exceptions.AttributeError Traceback (most recent
call last)
/home/maric/<ipython console>
AttributeError: can't set attribute
In [49]: ib=b()
In [50]: print ib.p
None
In [51]: ib.p = 5
In [52]: print ib.p
5
In [53]: ib._p
Out[53]: 5
--
_____________
Maric Michaud
_____________
Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
More information about the Python-list
mailing list