Advertisement
Mushi

decorator

Sep 19th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. import functools
  2.  
  3. def my_decorator(func):
  4.     @functools.wraps(func)
  5.     def function_that_runs_func():
  6.         print('In the decorator!')
  7.         func()
  8.         print("After decorator!")
  9.     return function_that_runs_func
  10.  
  11. @my_decorator
  12. def my_function():
  13.     print("I am the function!")
  14.  
  15. #my_function()
  16.  
  17. def decorator_with_arguments(number):
  18.     def my_decorator(func):
  19.         @functools.wraps(func)
  20.         def function_that_runs_func(*args, **kwargs):
  21.             print('In the decorator!')
  22.             if number == 56:
  23.                 print("Not running the function")
  24.             else:
  25.                 func(*args, **kwargs)
  26.             print("After decorator!")
  27.         return function_that_runs_func
  28.     return my_decorator
  29.  
  30. @decorator_with_arguments(57)
  31. def my_function_too(x, y):
  32.     print(x, y)
  33.  
  34. my_function_too(57, 67)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement