Skip to content

Instantly share code, notes, and snippets.

@yloiseau
Last active August 29, 2015 14:05
Show Gist options
  • Save yloiseau/c3d1013fadad9a4e2370 to your computer and use it in GitHub Desktop.
Save yloiseau/c3d1013fadad9a4e2370 to your computer and use it in GitHub Desktop.
# Decorator
def checkparams(*checkers):
def _deco_(fun):
def _wrapped_(*args):
print("I check {} params".format(fun.__name__))
assert all(f(v) for f,v in zip(checkers, args))
return fun(*args)
return _wrapped_
return _deco_
# Type checking function factory
checktype = lambda t: lambda v: isinstance(v, t)
@checkparams(checktype(int), checktype(str), checktype(float))
def foo(a, b, c):
print(":D")
foo(42, "foo", 13.37)
foo(13.37, 42, "foo")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment