Skip to content

Instantly share code, notes, and snippets.

@namoshizun
Created June 24, 2017 01:23
Show Gist options
  • Save namoshizun/f021847df928420125c82aa42a2b1da0 to your computer and use it in GitHub Desktop.
Save namoshizun/f021847df928420125c82aa42a2b1da0 to your computer and use it in GitHub Desktop.
def checktype(*types):
def typerror(expect, got):
raise TypeError('expecting {}, got{}'.format(expect, type(got)))
def checker(fn):
def receiver(*args, **kwargs):
for _type, arg in zip(types, args):
type(arg) is _type or typerror(_type, arg)
return fn(*args, **kwargs)
return receiver
return checker
@checktype(int, str)
def foo(num, word):
print(num ,word)
foo(1, '2')
# foo(1, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment