Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

subclassing complex

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


bidihall at gmail

Aug 28, 2008, 10:17 PM

Post #1 of 5 (50 views)
Permalink
subclassing complex

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?

Here's an example:

class xcomplex( complex ):

def __new__(cls,*args,**kwargs):
return complex.__new__(cls,*args,**kwargs)

def __coerce__(self,other):
t = complex.__coerce__(self,other)
try:
return (self,xcomplex(t[1]))
except TypeError:
return t

def __add__(self,x):
return xcomplex( complex.__add__(self,x) )

def __radd__(self,x):
return xcomplex( complex.__radd__(self,x) )


xz = xcomplex(1+2j)
xy = float(10.0)
z = complex(10+1j)

print type(xz + z) # OK: get xcomplex
print type(xz + xy) # OK: get xcomplex
print type(xz + 10) # OK: get xcomplex
print type(xy + xz) # OK: get xcomplex
print type(10 + xz) # OK: get xcomplex

print type(z + xz) # fails: get complex
--
http://mail.python.org/mailman/listinfo/python-list


pmaupin at gmail

Aug 29, 2008, 1:56 AM

Post #2 of 5 (44 views)
Permalink
Re: subclassing complex [In reply to]

On Aug 29, 12:17 am, BiDi <bidih...@gmail.com> 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?

I think the issue is that Python first tries to use the __add__ method
of the left-most object, and only attempts to use __radd__ with the
right-most object if that fails. Because you have subclassed the
complex class, the __add__ method of the complex number will work
fine, returning a complex result.

If you want to keep that from working, you probably want to just
inherit from 'object' rather than 'complex', and reimplement all the
methods you care about (possibly with a very simple wrapper around an
internal complex number).

Regards,
Pat
--
http://mail.python.org/mailman/listinfo/python-list


__peter__ at web

Aug 29, 2008, 2:24 AM

Post #3 of 5 (41 views)
Permalink
Re: subclassing complex [In reply to]

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
--
http://mail.python.org/mailman/listinfo/python-list


pmaupin at gmail

Aug 29, 2008, 11:18 AM

Post #4 of 5 (37 views)
Permalink
Re: subclassing complex [In reply to]

On Aug 29, 4:24 am, Peter Otten <__pete...@web.de> 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 http://docs.python.org/ref/numeric-types.html:

"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.)

Regards,
Pat
--
http://mail.python.org/mailman/listinfo/python-list


bidihall at gmail

Aug 29, 2008, 3:44 PM

Post #5 of 5 (36 views)
Permalink
Re: subclassing complex [In reply to]

On Aug 30, 6:18 am, Patrick Maupin <pmau...@gmail.com> wrote:
> On Aug 29, 4:24 am, Peter Otten <__pete...@web.de> 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 athttp://docs.python.org/ref/numeric-types.html:
>
> "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.)
>
> Regards,
> Pat

Thanks for the comments. I have filed it as an issue.

Regards

Blair
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.