Functions, Operators, and Overloading?

M

Michael Yanowitz

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
 
B

Brian Beck

Michael said:
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.
 
G

Gerhard Fiedler


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
 
S

Stefan Behnel

Gerhard said:
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
 
B

bearophileHUGS

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.jsp?forum=106&thread=101605
http://bob.pythonmac.org/archives/2005/03/30/five-minute-multimethods-in-python-using-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
 
G

Gerhard Fiedler

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
 
B

Bruno Desthuilliers

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/DevCenter/VisitorRevisited
However, operators can be overloaded.
So can I define a new operator?

No.
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top