Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Python
Posted by: alphaharris
Added: Sep 25, 2018 3:06 PM
Views: 8
  1. # Complex decorators can accept arguments
  2.  
  3. def decorator_with_arguments(number):
  4.     def my_decorator(func):
  5.         @functools.wraps(func)
  6.         def function_that_runs_func(*args, **kwargs):
  7.             print('In the decorator!')
  8.             if number == 56:  # use the argument that's passed in
  9.                 print("not running the function")
  10.             else:
  11.                 func(*args, **kwargs)
  12.             print('After the decorator!')
  13.  
  14.         return function_that_runs_func
  15.  
  16.     return my_decorator
  17.  
  18.  
  19. @decorator_with_arguments(57)
  20. def my_function_too(x, y):
  21.     print(x + y)
  22.  
  23.  
  24. my_function_too(57, 57)