473,395 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1670
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Terje Slettebø | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any...
45
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
31
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
15
by: lordkain | last post by:
is it possible to do some kind of function overloading in c? and that the return type is different
11
by: placid | last post by:
Hi all, Is it possible to be able to do the following in Python? class Test: def __init__(self): pass def puts(self, str): print str
10
by: Matthew | last post by:
Am I correct in thinking there is no method/function overloading of any kind in any version of PHP? Thanks, Matthew
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.