Why property works only for objects?

Michal Kwiatkowski ruby at no.spam
Sun Mar 12 11:13:43 EST 2006


Alex Martelli napisał(a):
>> Can you also check my reasoning for getting attributes?
>>
>> value = obj.attr
>>   * if instance class has __getattribute__, call it
>>   * else: lookup "attr" in all parent classes using class __mro__;
>>     if it's a descriptor call its __get__ method, return its value
>>     otherwise (when descriptor doesn't have __get__, it's unreadable
>>     and AttributeError is raised)
>>   * else: check instance __dict__ for "attr", return it when found
>>   * else: lookup __getattr__ in instance class and call it when found
>>   * else: raise AttributeError
> 
> No, the value found in the instance (your second 'else' here) takes
> precedence if the descriptor found in the first 'else' is
> non-overriding.

Oh, right. My mistake comes from the subtle difference between defining
descriptor as a class and by property() builtin (I've tested only second
option and assumed that descriptor without __set__ cannot be rebinded):

class non_overriding(object):
    def __get__(*a):
        return 12

class C(object):
    x = non_overriding()
    y = property(lambda s:23)

c = C()

c.x = 4
print c.x # => 4

c.y = 5  # => AttributeError: can't set attribute

IMHO that's not very consistent. Well, probably some code rely on this,
so I just have to live with it.

Thanks for your time and patience in explaining my doubts.

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