subclassing complex

Peter Otten __peter__ at web.de
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()
1j

versus

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

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

Peter



More information about the Python-list mailing list