Callable dispatcher that dispatches based on argument types
Usage:
from peak.util.dispatch import GenericFunction, Signature
class Foo: pass
class Bar(Foo): pass
class Baz(Foo): pass
floob = GenericFunction()
floob[Signature(Foo,Bar)] = lambda x,y: "foo, bar"
floob[Signature(Foo,Foo)] = lambda x,y: "foo, foo"
print floob(Foo(),Bar()) # closest match is Foo, Bar
print floob(Baz(),Baz()) # closes match is Foo, Foo
GenericFunction calls the function whose Signature is closest
to the classes of the supplied arguments. There is no way to
call the "next closest" function. (Note that although keyword
arguments are passed through to the function, they cannot be
used for dispatching.)
|