#!/usr/bin/env python
from functools import wraps
def overloaded(func):
@wraps(func)
def overloaded_func(*args, **kwargs):
for f in overloaded_func.overloads:
try:
return f(*args, **kwargs)
except TypeError:
pass
else:
# it will be nice if the error message prints a list of
# possible signatures here
raise TypeError("No compatible signatures")
def overload_with(func):
overloaded_func.overloads.append(func)
return overloaded_func
overloaded_func.overloads = [func]
overloaded_func.overload_with = overload_with
return overloaded_func
#############
@overloaded
def foo():
print 'foo() without args'
pass
@foo.overload_with
def _(n):
# note that, like property(), the function's name in
# the "def _(n):" line can be arbitrary, the important
# name is in the "@overloads(a)" line
print 'foo() with one argument'
pass
foo()
foo(4)
foo(4, 5) # ERROR: no matching signature
Diff to Previous Revision
--- revision 2 2010-02-23 15:20:19
+++ revision 3 2010-04-06 00:59:52
@@ -39,6 +39,6 @@
print 'foo() with one argument'
pass
-a()
-a(4)
-a(4, 5) # ERROR: no matching signature
+foo()
+foo(4)
+foo(4, 5) # ERROR: no matching signature