View Full Version : Begining python question, returning an object

Ramses de Norre
March 6th, 2007, 08:28 PM
Today I started looking into python a bit, after reading many good things about it.
I now have my first question about it, I wrote this little class:

#!/usr/bin/env python

class Complex:
"""Class to represent complex numbers"""

def __init__(self,a,b):
Takes two numbers as arguments,
the first is the real part of the complex number,
the second the imaginary one."""

def getReal(self):
"""Getter for the real part of this complex number"""
return self.a

def getImaginary(self):
"""Getter for the imaginary part of this complex number"""
return self.b

def __setReal__(self,a):
"""Setter for the real part of this complex number"""

def __setImaginary__(self,b):
"""Setter for the imaginary part of this complex number"""

def addTo(self,obj):
"""Returns a new object that is the complex sum of self and obj"""
return Complex(self.getReal()+Complex(obj).getReal(), self.getImaginary()+Complex(obj).getImaginary())

def toString(self):
"""Returns a string representation of this object"""
return `self.getReal()`+"+"+`self.getImaginary()`+'i'

print b.toString()
print c.toString()
print d.toString()
And I get this error when running it:
Traceback (most recent call last):
File "/home/ramses/code/java/workspace/FirstPythonTest/src/first/complex.py", line 45, in ?
File "/home/ramses/code/java/workspace/FirstPythonTest/src/first/complex.py", line 33, in addTo
return Complex(self.getReal()+Complex(obj).getReal(), self.getImaginary()+Complex(obj).getImaginary())
TypeError: __init__() takes exactly 3 arguments (2 given)

Why does it tell me to give three arguments to the constructor?? The first (self) is the object it creates, isn't it? How am I supposed to give that as an argument? And the same construction seems to work in the little driver code?

Can anyone point out what I'm missing?
Ow and I have programming and OOP experience, so I can understand decent explanations.

March 6th, 2007, 09:11 PM
When you say Complex(obj), you are calling the constructor with just one argument (which will become two when self is added).

March 6th, 2007, 09:16 PM
Change the line in addTo to

return Complex(self.getReal()+obj.getReal(), self.getImaginary()+obj.getImaginary())

and your program works.

Ramses de Norre
March 7th, 2007, 12:51 AM
So no form of typecasting? This seems so unsafe?
Maybe I need to get used a bit more to this dynamic typing stuff... Thanks for helping me out ;)