Overloading

  • Thread starter Johannes Reichel
  • Start date
J

Johannes Reichel

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!
 
J

jepler

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

Python doesn't support this, but it does support default arguments:
class Complex:
def __init__(self, real=0, imag=0):
self.real = real
self.imag = imag
And by the way, is it possible to overload operators like +,-,*?

def operator+(self,complex2):

The special methods have names like __add__.
http://docs.python.org/ref/numeric-types.html

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDmcM5Jd01MZaTXX0RAnxcAJ0Zr/Hs2aq23LPfjsAP1+1UbqdHqwCeJSU5
/d2dWKe9bPVJyC3MAVIAHq4=
=yb2f
-----END PGP SIGNATURE-----
 
C

Christopher Subich

Johannes said:
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:

Please do note, if you want this for the exact use of a Complex class,
Python does have complex arithmetic built-in:

print (0 + 1j)

The other posted points are still valid (and generally applicable).
 
J

Josef Meile

In C++ you can overload functions and constructors. For example if I have a
Python doesn't support this, but it does support default arguments:
Yes, in part you are right since the python core doesn't include them.
However they can be implemented with decorators:

* Subject: decorators and multimethods
Group: comp.lang.python
Link: http://tinyurl.com/d45ym

Anyway, as you, I also use the default arguments.

Regards
Josef
 
J

Josef Meile

In C++ you can overload functions and constructors. For example if I
Yes, in part you are right since the python core doesn't include them.
However they can be implemented with decorators:

* Subject: decorators and multimethods
Group: comp.lang.python
Link: http://tinyurl.com/d45ym
There is even a better recipe, explained step by step from Guido:
* Five-minute Multimethods in Python
Link: http://tinyurl.com/8ppd6

Regards
Josef
 

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
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top