Functions, Operators, and Overloading?

Gerhard Fiedler gelists at gmail.com
Mon Jul 24 15:14:33 EDT 2006


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




More information about the Python-list mailing list