473,324 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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
Aug 29 '08 #1
4 1428
On Aug 29, 12:17*am, BiDi <bidih...@gmail.comwrote:
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
Aug 29 '08 #2
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
Aug 29 '08 #3
On Aug 29, 4:24 am, Peter Otten <__pete...@web.dewrote:
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
Aug 29 '08 #4
On Aug 30, 6:18*am, Patrick Maupin <pmau...@gmail.comwrote:
On Aug 29, 4:24 am, Peter Otten <__pete...@web.dewrote:
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
Aug 29 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: WhiteRavenEye | last post by:
Why can't I subclass any window except mine in VB? Do I have to write dll for this? I've tried to subclass it with SetWindowLong but without success... Does anyone know how to subclass window...
2
by: BJörn Lindqvist | last post by:
A problem I have occured recently is that I want to subclass builtin types. Especially subclassing list is very troublesome to me. But I can't find the right syntax to use. Take for example this...
3
by: Peter Olsen | last post by:
I want to define a class "point" as a subclass of complex. When I create an instance sample = point(<arglist>) I want "sample" to "be" a complex number, but with its real and imaginary...
6
by: gregory lielens | last post by:
Hello, I am currently writing python bindings to an existing C++ library, and I just encountered a problem that I hope has been solved by more experienced python programmers: A C++ class...
11
by: Brent | last post by:
I'd like to subclass the built-in str type. For example: -- class MyString(str): def __init__(self, txt, data): super(MyString,self).__init__(txt) self.data = data
11
by: Michael Rodriguez | last post by:
I'm trying to figure out how to subclass a control and override some of the default properties. I can subclass a control and add my own new properties and set their default values. But how do you...
10
by: Frank Millman | last post by:
Hi all I recently posted a question about subclassing. I did not explain my full requirement very clearly, and my proposed solution was not pretty. I will attempt to explain what I am trying to...
16
by: manatlan | last post by:
I've got an instance of a class, ex : b=gtk.Button() I'd like to add methods and attributes to my instance "b". I know it's possible by hacking "b" with setattr() methods. But i'd like to do...
5
by: Ray | last post by:
Hi all, I am thinking of subclassing the standard string class so I can do something like: mystring str; .... str.toLower (); A quick search on this newsgroup has found messages by others
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.