This (just for fun!) decorator makes python functions polymorphic. One thing I like about C++ is polymorphism. I like to be able to do this:
void do_something(const Car &car); void do_something(const Aardvark &aard);
But something like this isn't possible with python, mainly because the only way to provide polymorphism is to check the incoming types, and python doesn't enforce types.
So for fun, I wanted to see if polymorphism could be implemented with a decorator. Turns out it can:
from polymorph import polymorph @polymorph(float) def wat(arg1): return "called with float" @polymorph(int, str) def wat(arg1, arg2): return "called with int and str" @polymorph(str, str) def wat(arg1, arg2): return "called with str and str" print wat(2.0) print wat(1, "test") print wat("yes", "test") print wat(1, 2) # raises
called with float called with int and str called with str and str TypeError: wat() has no signature (<type 'int'>, <type 'int'>)