Why property works only for objects?
Michal Kwiatkowski
ruby at no.spam
Fri Mar 10 12:01:16 EST 2006
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?!
Statically typed languages background. Sorry. ;)
>> Code below doesn't work, but shows my
>> intention:
>>
>> # obj is instance of BaseClass
>> def get_x(self):
>> # ...
>> def set_x(self, value):
>> # ...
>> obj.x = property(get_x, set_x)
>
> 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?
mk
--
. o . >> http://joker.linuxstuff.pl <<
. . o It's easier to get forgiveness for being wrong
o o o than forgiveness for being right.
More information about the Python-list
mailing list