[Python-Dev] New style classes and operator methods

Armin Rigo arigo at tunes.org
Mon Apr 11 22:47:42 CEST 2005

Hi Greg,

On Fri, Apr 08, 2005 at 05:03:42PM +1200, Greg Ewing wrote:
> If the left and right operands are of the same class,
> and the class implements a right operand method but
> not a left operand method, the right operand method
> is not called. Instead, two attempts are made to call
> the left operand method.

This is not a general rule.  The rule is that if both elements are of the same
class, only the non-reversed method is ever called.  The confusing bit is
about having it called twice.  Funnily enough, this only occurs for some
operators (I think only add and mul).  The reason is that internally, the C
core distinguishes about number adding vs sequence concatenation, and number
multiplying vs sequence repetition.  So __add__() and __mul__() are called
twice: once as a numeric computation and as a sequence operation...

Could be fixed with more strange special cases in abstract.c, but I'm not sure
it's worth it.


More information about the Python-Dev mailing list