Last active
August 29, 2015 14:05
-
-
Save yloiseau/c3d1013fadad9a4e2370 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
# 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