Decorators are a way to modify functions and classes at runtime.
Higher-order functions accepts another function or returns another function.
Here is an example of writing a decorator.
def my_decorator(func): def wrap_func(): print('*****') func() print('*****') return wrap_func @my_decorator def hello(): print('Hello') hello()
Handling decorators with args.
def my_decorator(func): def wrap_func(*args, **kwargs): print('*****') func(*args, **kwargs) print('*****') return wrap_func @my_decorator def hello(): print('Hello') hello()
Examples:
performance
decorator to test a code.auth
decorator to check if a user is authenticated.logging
decorator to log a function call.