subclassing complex

Peter Otten __peter__ at
Fri Aug 29 11:24:29 CEST 2008

BiDi wrote:

> I have been trying to subclass complex, but I am not able to get the
> right-hand arithmetic operators working.
> As shown below, if an object of my subclass 'xcomplex' is added on the
> right of a 'comlex' object, the type returned is 'complex', not
> 'xcomplex'.
> I've tried subclassing float and it works fine (don't even need to
> define __coerce__ in that case)
> Is this a bug, or am I missing something?

A minimal example is

>>> class Complex(complex):
...     def __radd__(self, other): print "radd"
>>> 1j + Complex()


>>> class Int(int):
...     def __radd__(self, other): print "radd"
>>> 1 + Int()

I think the complex subclass should behave like the int subclass.
To get an authoritative answer you should file a bug report.


More information about the Python-list mailing list