Functions, Operators, and Overloading?
Brian Beck
exogen at gmail.com
Mon Jul 24 13:30:31 EDT 2006
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
More information about the Python-list
mailing list