Signature-based Function Overloading in Python

Arnaud Delobelle arnodel at googlemail.com
Tue Feb 23 15:35:04 EST 2010


Michael Rudolf <spamfresser at ch3ka.de> writes:

> Just a quick question about what would be the most pythonic approach
> in this.
>
> In Java, Method Overloading is my best friend, but this won't work in
> Python:
>
>>>> def a():
> 	pass
>>>> def a(x):
> 	pass
>>>> a()
> Traceback (most recent call last):
>   File "<pyshell#12>", line 1, in <module>
>     a()
> TypeError: a() takes exactly 1 argument (0 given)
>
> So - What would be the most pythonic way to emulate this?
> Is there any better Idom than:
>
>>>> def a(x=None):
> 	if x is None:
> 		pass
> 	else:
> 		pass
>

There are a number of frameworks for function overloading out there.  FWIW,
there is actually one in the Python sandbox (for Python 3):

http://svn.python.org/view/sandbox/trunk/Overload3K/

-- 
Arnaud




More information about the Python-list mailing list