Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. def typecheck(*tuples, **kwtuples):
  2. def decorator(func):
  3. def function_wrapper(*args, **kwargs):
  4.  
  5. #print('tuples : ' , tuples)
  6. #print('kwtuples : ' , kwtuples)
  7. #print('args : ' , args)
  8. #print('kwargs : ' , kwargs)
  9.  
  10. for index, tup in enumerate(tuples):
  11. arg = args[index]
  12. if not isinstance(arg, tup):
  13. 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)) )
  14.  
  15. for key, tup in kwtuples.items():
  16. arg = kwargs[key]
  17. if not isinstance(args[index], tup):
  18. raise ValueError('in ' + str(func.__name__) + ' : wrong argument ' + str(key) + ' :' + str(arg) + ' must be of type :' + str(tup) + 'but is' + str(type(arg)) )
  19.  
  20.  
  21.  
  22.  
  23. #print("arguments: ", *args, **kwargs)
  24. func(*args, **kwargs)
  25. return function_wrapper
  26. return decorator
  27.  
  28. @typecheck(str,(str, float))
  29. def foo2(x,y):
  30. print("Hi, foo has been called with ",str(x) ,y)
  31.  
  32. foo2('2',3.4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement