Skip to content

Instantly share code, notes, and snippets.

@aaronchall
Created September 23, 2019 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronchall/2e9f869625ef51e61b76969c9d43b09e to your computer and use it in GitHub Desktop.
Save aaronchall/2e9f869625ef51e61b76969c9d43b09e to your computer and use it in GitHub Desktop.
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