Overloading operators on the rigth.

Bengt Richter bokr at oz.net
Fri Oct 15 03:34:00 CEST 2004

On Fri, 15 Oct 2004 10:55:59 +1100, Andrew Durdin <adurdin at gmail.com> wrote:

>On Thu, 14 Oct 2004 20:27:13 GMT, Bengt Richter <bokr at oz.net> wrote:
>> BTW, I experimented a little further with NotImplemented and the gt,cmp,coerce
>> methods in a base class vs derived and it looks like the search prefers
>> a base class gt or cmp over a derived version of the next alternative.
>Isn't this just a side-effect of the attribute lookup order? When
>looking for __gt__, python'll check the class, then go through the
>base classes, and only return AttributeError if it's not found in the
>class or any of its bases; after which it tries __cmp__ etc.
>Am I understanding this correctly?
I think so, yes. I was just trying to point out that it might not be
the really logical thing to do. I.e., if a programmer supplies a __cmp__
method in his derived class, why would he do that if he didn't expect it
to be used for comparisons with his special kind of instances? Should he
have to know that __gt__ in a base class will preempt his intent, as e.g.
in the case of 2 < derived_instance?

I think, as you say, it's a side-effect of attribute lookup order, but
IWT that semantics should demand looking for all alternatives before going
to the next base, and then doing the same at each stage. That's probably
a pain to implement though ;-)

Bengt Richter

More information about the Python-list mailing list