Signature-based Function Overloading in Python

Michael Rudolf spamfresser at ch3ka.de
Wed Feb 24 06:42:08 EST 2010


First: Thanks for all the replies so far, they really helped me.

Am 24.02.2010 11:28, schrieb Jean-Michel Pichavant:
>> >>> def a(x=None):
>>     if x is None:
>>         pass
>>     else:
>>         pass
>>
> This is the way to do it python, and it has its advantages: 1 docstring,
> 1 way do do it, 1 interface.

Yes, I see. Actually I do now realize that even in Java I use method 
overloading mostly to implement optional arguments anyway, like:

void constructor(){this.foo='foo'; this.initotherstuff();}
void constructor(int x) {this.x=x; this.constructor();}

and so on.

So most of the time the idiom above is exactly what I need, as the 
versions of the function share code anyway.

But there are also cases where they do something completely different - 
in these cases I might use one of the other solutions provided here or 
simply make two or three functions and name them appropiately.

I do now see that the pythonic approach is the "best" for most cases, 
but I really loved to see that you *can* do overloading in a convenient 
way if you really want to :D Those decorators just rock :D

Thanks again,
Michael



More information about the Python-list mailing list