Functions, Operators, and Overloading?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Functions, Operators, and Overloading?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Functions, Operators, and Overloading?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Functions, Operators, and Overloading?
Michael Yanowitz [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] 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. I am hard put to think of why you would want to do something like that in real code. The 2nd-parameter type mismatch will make using code harder to write and read without bugs. Why not just give the two different-but-related functions two different-but-related, names? But if you insist ..., here is the Python way... def func1(int1, int3, string1=None, string2=None): doc short calling sequence if string1==None: string1 = int3; del int3, string2 proceed with first body else: proceed with second body or def func1(int1, *args): if len(args ==1): string1 = args[0]; del args proceed with first body elif len(args ==3): int3,string1,string2 = args; del args proceed with second body else: raise error However, operators can be overloaded. In the sense that you can define classes with appropriate special methods. You can also give two different classes methods named 'func1'. So can I define a new operator? No. Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Functions, Operators, and Overloading?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Functions, Operators, and Overloading?
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=106thread=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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Functions, Operators, and Overloading?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Functions, Operators, and Overloading?
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. -- http://mail.python.org/mailman/listinfo/python-list