Created
May 2, 2021 12:26
-
-
Save raeq/45110c78d10e4f0f27c6cde950b761c8 to your computer and use it in GitHub Desktop.
Which dispatcher will be called
This file contains hidden or 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
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