Riju21

29_decorator

May 12th, 2019
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. # decorator
  2. # -------------
  3.  
  4.  
  5. # normal example
  6. # ------------------
  7.  
  8. # def sqr(i):
  9. #     return i * i
  10.  
  11. # def calc(calcFun, numArr):
  12. #     result = []
  13. #     for i in numArr:
  14. #         result.append(calcFun(i))
  15. #     return result    
  16.  
  17. # numbers = calc(sqr, [1, 2, 3, 4, 5])
  18. # print(numbers)
  19.  
  20. # original decorator example
  21. # --------------------------------------------
  22.  
  23. def decorator_xmple(main_fun):
  24.     def wrapper(*args, **kargs):
  25.         print('this is printed before {} function'.format(main_fun.__name__))
  26.         return main_fun(*args, **kargs)
  27.     return wrapper
  28.  
  29.  
  30. @decorator_xmple
  31. def msg():
  32.     print('now i got it .. . . . . ')
  33.  
  34. msg()
  35.  
  36. @decorator_xmple
  37. def person(name, age):
  38.     print('name & age is: {} , {}'.format(name, age))
  39. person('riju', 21)    
  40. # passThrough = decorator_xmple(msg)
  41. # print(passThrough)
Advertisement
Add Comment
Please, Sign In to add comment