Skip to content

@lucidguppy /Type check
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.