A newbie question

W

wang frank

Hi,

I am trying to write a python class with a new data type such as:
class Cc14:
def __init__(self, realpart, imagpart):
self.r=realart
self.i=imagpart

def __add__(self,x):
return self.r+x,r, self.i+x.i

If I have
x=Cc14(4,5)
y=Cc14(4,5)
z=x+y

z will be a tuple instead of Cc14. How can I return a Cc14 class?

Thanks
Frank

_________________________________________________________________
$B%&%'%V%Z!<%8$r0u:~$7$F$bES@Z$l$J$$!*JXMx$J%V%i%&%6(B MSN$BHG(BIE7 $B$r;H$*$&(B
http://promotion.msn.co.jp/ie7/
 
M

Matimus

How about this:
Code:
    def __add__(self,x):
        return Cc14(self.r+x.r, self.i+x.i)

However... Are you aware that Python has built in support for complex
numbers already?
 
D

Dan Bishop

Hi,

I am trying to write a python class with a new data type such as:
class Cc14:
def __init__(self, realpart, imagpart):
self.r=realart
self.i=imagpart

def __add__(self,x):
return self.r+x,r, self.i+x.i

If I have
x=Cc14(4,5)
y=Cc14(4,5)
z=x+y

z will be a tuple instead of Cc14. How can I return a Cc14 class?

return Cc14(self.r+x,r, self.i+x.i)

FYI, Python has a built-in "complex" type.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top