Last active
December 23, 2015 08:19
-
-
Save omaraboumrad/6607039 to your computer and use it in GitHub Desktop.
Poor man's singledispatch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define | |
def singledispatch(func): | |
through_map = {} | |
def register(through): | |
def _on(what): | |
through_map[through] = what | |
return what | |
return _on | |
def dispatch(what): | |
return through_map.get(what, func) | |
def run(arg): | |
return through_map.get(type(arg), func)(arg) | |
run.register = register | |
run.dispatch = dispatch | |
return run | |
# Use | |
@singledispatch | |
def fun(arg): | |
return 'generic: {}'.format(arg) | |
@fun.register(str) | |
def _str(arg): | |
return 'str: {}'.format(arg) | |
@fun.register(int) | |
def _int(arg): | |
return 'int: {}'.format(arg) | |
@fun.register(float) | |
def _(arg): | |
return 'float: {}'.format(arg) | |
# Test | |
assert fun(1) == 'int: 1' | |
assert fun(1.0) == 'float: 1.0' | |
assert fun('omar') == 'str: omar' | |
assert fun([1,2,3]) == 'generic: [1, 2, 3]' | |
assert fun.dispatch(int) == _int |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment