Bug overriding operators in new-style classes?
Nicodemus
nicodemus at globalite.com.br
Fri Jul 18 19:19:55 EDT 2003
Hi Steven,
Steven Taschuk wrote:
>Quoth Nicodemus:
>
>
>>I found a surprising behavior regarding new-style classes operator lookup.
>>It seems that for operators, the instance methods are ignored. Observe:
>>
>>
> [...]
>
>
>>Is this a bug, or am I missing something? Any help would be appreciated.
>>
>>
>
>Working as designed, I think [1], though I've never actually seen
>it documented. As you have observed, invocation of a magic method
>(such as __add__ or __len__) by way of a special notation (such as
>+ or len()) ignores the instance dict. That is,
> len(x)
>is not equivalent to
> x.__len__()
>(which would find __len__ in the instance dict by normal attribute
>access) but to
> type(x).__len__(x)
>(which obviously ignores the instance dict).
>
>[...]
>
>[1] One reason I think it's intended is that these protocols
>specify a self argument; for new-style classes, this is provided
>by the descriptor machinery, which by design works only when the
>function is found in the class dict.
>
>
Thanks for the clarification!
Regards,
Nicodemus.
More information about the Python-list
mailing list