Skip to content

Instantly share code, notes, and snippets.

@lucidguppy
Created March 8, 2014 12:41
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 lucidguppy/9429971 to your computer and use it in GitHub Desktop.
Save lucidguppy/9429971 to your computer and use it in GitHub Desktop.
Bare bones type checking for function
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