subclassing complex

Patrick Maupin pmaupin at
Fri Aug 29 20:18:38 CEST 2008

On Aug 29, 4:24 am, Peter Otten <__pete... at> wrote:
> 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.

Hmm, good point.  I shouldn't look at newsgroups when I'm too tired to
see the whole problem.

According to the documentation at

"Note: If the right operand's type is a subclass of the left operand's
type and that subclass provides the reflected method for the
operation, this method will be called before the left operand's non-
reflected method. This behavior allows subclasses to override their
ancestors' operations."

I think this makes it pretty clear that the OP found a bug in how
complex works.  (Before I read this note, I would have assumed that
the int() handling was broken, but it looks like a supportable design
decision.  Probably whoever implemented it wasn't even thinking about
complex numbers, but for consistency, I would think they should be
made to work the same.)


More information about the Python-list mailing list