Why property works only for objects?
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Fri Mar 10 21:56:03 EST 2006
Michal Kwiatkowski a écrit :
> Alex Martelli napisał(a):
>
>>Wrong! Of _course_ it's an option -- why do you think it matters at all
>>whether you're the creator of this object?!
>
(snip)
>>
>>def insert_property(obj, name, getter, setter):
>> class sub(obj.__class__): pass
>> setattr(sub, name, property(getter, setter))
>> obj.__class__ = sub
>>
>>See? Of COURSE you can subclass -- not hard at all, really.
>
>
> Let me understand it clearly. If I change __class__ of an object,
> existing attributes (so methods as well)
> of an object are still
> accessible the same way and don't change its values. Only resolution of
> attributes/methods not found in object is changed, as it uses new
> version of __class__ to lookup names. Is this right?
Attributes, yes. Not methods. Methods are looked up in the class. But in
the example above (brillant, as usual), Alex dynamically creates a
subclass of obj.__class__, so inheritence takes care of methods lookup.
> mk
More information about the Python-list
mailing list