Created
September 23, 2019 19:37
-
-
Save aaronchall/2e9f869625ef51e61b76969c9d43b09e to your computer and use it in GitHub Desktop.
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
def api(arg): | |
if isinstance(arg, (str, bytes)): | |
if isinstance(arg, bytes): | |
arg = arg.decode('utf8') | |
arg = [arg] | |
... # do something | |
return arg | |
print(api(b'123')) # -> ['123'] | |
######## Compare and contrast above with below | |
from functools import singledispatch | |
@singledispatch | |
def api(arg): | |
... # do something | |
return arg | |
@api.register(str) | |
def _(arg): | |
return api([arg]) | |
@api.register(bytes) | |
def _(arg): | |
return api(arg.decode('utf8')) | |
print(api(b'123')) # -> ['123'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment