Overloading

Johannes Reichel reichel.johannes at t-online.de
Fri Dec 9 12:29:12 EST 2005


Hi!

In C++ you can overload functions and constructors. For example if I have a
class that represents a complex number, than it would be nice if I can
write two seperate constructors

class Complex:

def __init__(self):
        self.real=0
        self.imag=0

def __init__self(self,r,i):     
        self.real=r
        self.imag=i
        

How would I do this in python?

And by the way, is it possible to overload operators like +,-,*?

def operator+(self,complex2):
        Complex res
        res.real=self.real+complex2.real
        res.imag=self.imag+complex2.imag
        
        return res


thanks! 



More information about the Python-list mailing list