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: 3
Tags: decorator
  1. # Simple decorator
  2.  
  3. import functools
  4.  
  5.  
  6. # takes a function as a variable
  7. def my_decorator(func):
  8.     @functools.wraps(func)
  9.     def function_that_runs_func():
  10.         print('In the decorator!')
  11.         func()
  12.         print('After the decorator!')
  13.  
  14.     return function_that_runs_func
  15.  
  16.  
  17. # use the decorator
  18. @my_decorator
  19. def my_function():
  20.     print("I'm the function!")
  21.  
  22.  
  23. my_function()
  24.