Created
March 8, 2014 12:41
-
-
Save lucidguppy/9429971 to your computer and use it in GitHub Desktop.
Bare bones type checking for function
This file contains 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
from functools import wraps | |
class Boat(object): | |
def __init__(self,color): | |
self.color = color | |
def hello(self): | |
print("I'm a {} boat".format(self.color)) | |
def arg_check(typelist): | |
def decorator(f): | |
@wraps(f) | |
def wrapper(*args, **kwds): | |
for ii, typeitem in enumerate(typelist): | |
assert(isinstance(args[ii],typeitem[1])),\ | |
"{} is not {} for {}".format(args[ii] , typeitem[1],f) | |
return f(*args, **kwds) | |
return wrapper | |
return decorator | |
@arg_check((('myarg1',str),("myarg2",int),("boat",Boat))) | |
def silly(myarg1,myarg2,boat): | |
print(myarg1*myarg2) | |
boat.hello() | |
print("I'm done") | |
if __name__ == "__main__": | |
print("HELLO WORLD") | |
yacht = Boat("gray") | |
silly("gravy",3,yacht) | |
silly("gravy",3,"not a boat") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment