[Python-Dev] Operator overloading inconsistency (bug or feature?)
Guido van Rossum
guido@python.org
Thu, 08 Aug 2002 10:54:55 -0400
> Suppose that a new-style class wants to overload "*" and it
> defines two methods like this:
>
> class Foo(object):
> def __mul__(self,other):
> print "__mul__"
> def __rmul__(self,other):
> print "__rmul__"
>
> Python-2.2.1, if you try this, you get the following behavior:
>
> >>> f = Foo()
> >>> f*1.0
> __mul__
> >>> 1.0*f
> __rmul__
> >>> f*1
> __mul__
> >>> 1*f
> __mul__
>
> So here is the question: Why does the last statement in this example
> not invoke __rmul__? In other words, why do "1.0*f" and "1*f" produce
> different behavior. Is this intentional? Is this documented someplace?
> Is there a workaround? Or are we just missing something obvious?
Aargh. I *think* this may have to do with the hacks for sequence
repetition. But I'm not sure. A debug session tracing carefully
through the code is in order.
--Guido van Rossum (home page: http://www.python.org/~guido/)