Signature-based Function Overloading in Python

Diez B. Roggisch deets at nospam.web.de
Wed Feb 24 18:05:35 EST 2010


Am 24.02.10 12:42, schrieb Michael Rudolf:
> 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

You can do, see packages such as PEAK rules. They offer not only 
type-based overloading, but also value-based.

Diez



More information about the Python-list mailing list