Created
May 2, 2021 12:38
-
-
Save raeq/4c463c449b05b9b9efe4c9619d89c7d9 to your computer and use it in GitHub Desktop.
Which Dispatcher
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 | |
| def _floats(s: float): | |
| 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