Skip to content

Instantly share code, notes, and snippets.

@raeq
Created May 2, 2021 12:26
Show Gist options
  • Save raeq/45110c78d10e4f0f27c6cde950b761c8 to your computer and use it in GitHub Desktop.
Save raeq/45110c78d10e4f0f27c6cde950b761c8 to your computer and use it in GitHub Desktop.
Which dispatcher will be called
from functools import singledispatch
@singledispatch
def fancy_print(s):
raise NotImplementedError
@fancy_print.register(int)
def _ints(s):
print(s * 2)
@fancy_print.register(list)
def _lists(s):
for i, e in enumerate(s):
print(i, e)
@fancy_print.register(float)
def _floats(s):
print(round(s, 2))
print(fancy_print.dispatch(int))
print(fancy_print.dispatch(list))
print(fancy_print.dispatch(float))
print(fancy_print.dispatch(dict))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment