Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def typecheck(*tuples, **kwtuples):
- def decorator(func):
- def function_wrapper(*args, **kwargs):
- #print('tuples : ' , tuples)
- #print('kwtuples : ' , kwtuples)
- #print('args : ' , args)
- #print('kwargs : ' , kwargs)
- for index, tup in enumerate(tuples):
- arg = args[index]
- if not isinstance(arg, tup):
- raise ValueError('in ' + str(func.__name__) + ' : wrong argument on position ,' + str(index) + ' :' + str(arg) + ' must be of type :' + str(tup) + 'but is' + str(type(arg)) )
- for key, tup in kwtuples.items():
- arg = kwargs[key]
- if not isinstance(args[index], tup):
- raise ValueError('in ' + str(func.__name__) + ' : wrong argument ' + str(key) + ' :' + str(arg) + ' must be of type :' + str(tup) + 'but is' + str(type(arg)) )
- #print("arguments: ", *args, **kwargs)
- func(*args, **kwargs)
- return function_wrapper
- return decorator
- @typecheck(str,(str, float))
- def foo2(x,y):
- print("Hi, foo has been called with ",str(x) ,y)
- foo2('2',3.4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement