469,306 Members | 1,850 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

Overloading

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!
Dec 9 '05 #1
4 1557
On Fri, Dec 09, 2005 at 06:29:12PM +0100, Johannes Reichel wrote:
Hi!

In C++ you can overload functions and constructors. For example if I havea
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-----

Dec 9 '05 #2
Johannes Reichel wrote:
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).
Dec 9 '05 #3
>>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:

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

Dec 9 '05 #4
>>> 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:


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

Dec 9 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Terje Slettebų | last post: by
4 posts views Thread by Dave Theese | last post: by
5 posts views Thread by | last post: by
39 posts views Thread by zeus | last post: by
45 posts views Thread by JaSeong Ju | last post: by
31 posts views Thread by | last post: by
15 posts views Thread by lordkain | last post: by
11 posts views Thread by placid | last post: by
10 posts views Thread by Matthew | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.