By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,532 Members | 1,348 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,532 IT Pros & Developers. It's quick & easy.

Functions, Operators, and Overloading?

P: n/a
Hello:

Maybe I am missing something, but from what I've seen,
it is not possible to overload functions in Python. That
is I can't have a
def func1 (int1, string1):
and a
def func1 (int1, int3, string1, string2):
without the second func1 overwriting the first.

However, operators can be overloaded.
So can I define a new operator? If so, can I
define func1 as an operator?

(on the side, I have always wanted to define the
++ operator as +=1. Is that possible?)

Thanks in advance:
Michael Yanowitz

Jul 24 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Michael Yanowitz wrote:
Maybe I am missing something, but from what I've seen,
it is not possible to overload functions in Python. That
is I can't have a
def func1 (int1, string1):
and a
def func1 (int1, int3, string1, string2):
without the second func1 overwriting the first.
Correct.
However, operators can be overloaded.
So can I define a new operator? If so, can I
define func1 as an operator?
No. Operators in Python are merely syntax for "magic methods" on the
corresponding type. For instance, x + y would call x.__add__(y). In this
sense you are not really "overloading" the operator, you are simply
defining or overwriting its behavior (just like above, where the second
func1 overwrites the previous).
(on the side, I have always wanted to define the
++ operator as +=1. Is that possible?)
No, for the reason above -- there is no magic method associated with ++,
which isn't a real Python operator.

--
Brian Beck
Adventurer of the First Order
Jul 24 '06 #2

P: n/a
On 2006-07-24 14:30:31, Brian Beck wrote:
Michael Yanowitz wrote:
> Maybe I am missing something, but from what I've seen,
it is not possible to overload functions in Python. That
is I can't have a
def func1 (int1, string1):
and a
def func1 (int1, int3, string1, string2):
without the second func1 overwriting the first.

Correct.
Can you write a function that accepts any number of arguments? And then
branch based on the number of arguments supplied?

I guess you can do that with a list as only argument. But can that be done
using the "normal" function argument notation?

Gerhard

Jul 24 '06 #3

P: n/a
Gerhard Fiedler schrieb:
On 2006-07-24 14:30:31, Brian Beck wrote:
>Michael Yanowitz wrote:
>> Maybe I am missing something, but from what I've seen,
it is not possible to overload functions in Python. That
is I can't have a
def func1 (int1, string1):
and a
def func1 (int1, int3, string1, string2):
without the second func1 overwriting the first.
Correct.

Can you write a function that accepts any number of arguments? And then
branch based on the number of arguments supplied?

I guess you can do that with a list as only argument. But can that be done
using the "normal" function argument notation?
I guess you mean something like

func1(int1, arg2, *args):
if len(args) == 2:
...
elif not args:
...
else:
raise ...
Stefan
Jul 24 '06 #4

P: n/a
Michael Yanowitz:
Maybe I am missing something, but from what I've seen,
it is not possible to overload functions in Python.
Maybe here you can find some ideas:
http://www.artima.com/forums/flat.js...&thread=101605
http://bob.pythonmac.org/archives/20...sing-dispatch/
http://blog.ianbicking.org/more-on-multimethods.html

(on the side, I have always wanted to define the
++ operator as +=1. Is that possible?)
That's not possible. Maybe you can create an inc() function, similar to
the Pascal one, that with a bit of stack-based magic may increment the
value its called on, but I think this is a bad idea. Leading/trailing
++/-- are quite bad for a language that tries to be clear and to avoid
programmer errors.

Bye,
bearophile

Jul 24 '06 #5

P: n/a
On 2006-07-24 15:05:53, Stefan Behnel wrote:
>>> Maybe I am missing something, but from what I've seen,
it is not possible to overload functions in Python. That
is I can't have a
def func1 (int1, string1):
and a
def func1 (int1, int3, string1, string2):
without the second func1 overwriting the first.
Correct.

Can you write a function that accepts any number of arguments? And then
branch based on the number of arguments supplied?

I guess you can do that with a list as only argument. But can that be done
using the "normal" function argument notation?

I guess you mean something like

func1(int1, arg2, *args):
if len(args) == 2:
...
elif not args:
...
else:
raise ...
Exactly. So in a way, the OP's question whether such an overload is
possible has two answers: a formal overload (two separate function
definitions) is not possible, but a functional overload (two definitions
for the different parameter numbers inside one function) is possible.

Thanks,
Gerhard

Jul 24 '06 #6

P: n/a
Michael Yanowitz a écrit :
Hello:

Maybe I am missing something, but from what I've seen,
it is not possible to overload functions in Python. That
is I can't have a
def func1 (int1, string1):
and a
def func1 (int1, int3, string1, string2):
without the second func1 overwriting the first.
You may want to have a look at Philip Eby's dispatch package. Here's an
introduction:
http://peak.telecommunity.com/DevCen...sitorRevisited
However, operators can be overloaded.
So can I define a new operator?
No.

Jul 24 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.