property and virtuality

Diez B. Roggisch deets.nospaaam at web.de
Thu Mar 31 07:00:21 EST 2005


> I cannot override C2._getname instead, because c2.name would print
> 'Test2" instead of lala. Clearly, the property stores a reference to the
> get and set methods and it is not possible to have it use the new
> methods.  Creating a new property is the worst - need to duplicate code
> and also C3.name is C1.name returns False. :-) It is not a big problem
> because I found the solution just I wonder if there is a better way to
> "virtualize" property get/set functions.

I'm not aware of possibility that works as you first expected. You yourself
explained why.

But _maybe_ you can use lambda here - that creates the layer  of indirection
one needs.

foo = property(lambda self: self.get_foo(), lamda self,v: self.set_foo(v))

On second thoughts, a metaclass _might_ help here - but it would be rather
elaborate: look in the baseclasses for properties that have getters and
setters of the same name as some methods in the current class, and replace
them, or  create a new property with them (I'm not sure if descriptors
allow changing their get/set/del methods). I'm not 100% sure if and how
good that works (a quick hack would be easy, but to ship around the cliffs
of multiple inheritance requires more careful navigation I fear...)

-- 
Regards,

Diez B. Roggisch



More information about the Python-list mailing list