Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A just-for-fun decorator that provides polymorphism to python
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.

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

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'>)
Something went wrong with that request. Please try again.