Advertisement
cd62131

python decorator with exec()

Apr 26th, 2019
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. class A:
  2.     def b(self, n):
  3.         def deco(fun):
  4.             def deco_sub(*args, **kwargs):
  5.                 print(f'--deco begin with b({n})')
  6.                 fun(*args, **kwargs)
  7.  
  8.             return deco_sub
  9.  
  10.         return deco
  11.  
  12.  
  13. a0 = A()
  14. a1 = A()
  15. a2 = A()
  16.  
  17.  
  18. @a0.b(11)
  19. def m0(v):
  20.     print(str(m0))
  21.     print(v)
  22.  
  23.  
  24. @a1.b(13)
  25. def m1(v):
  26.     print(str(m1))
  27.     print(v)
  28.  
  29.  
  30. @a2.b(17)
  31. def m2(v):
  32.     print(str(m2))
  33.     print(v)
  34.  
  35.  
  36. m0(10)
  37. m1(11)
  38. m2(12)
  39.  
  40. for i in range(3):
  41.     exec(f'''@a{i}.b(1)
  42. def m{i}(v):
  43.    print(str(m{i}))
  44.    print(v * 100)
  45. ''')
  46.  
  47. m0(10)
  48. m1(11)
  49. m2(12)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement