Signature-based Function Overloading in Python
Jean-Michel Pichavant
jeanmichel at sequans.com
Thu Feb 25 05:32:30 EST 2010
Michael Rudolf wrote:
> 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
You said it yourself: "simply make two or three functions and name them
appropiately" :-)
When 2 methods of a class were to have the same name for doing
completely different things like you said, there's a design flaw to my
opinion.
JM
More information about the Python-list
mailing list