from functools import wraps
12 Decorator
12.1 Concept
def outer_func(msg):
def inner_func():
print(msg)
return inner_func
Traditional function factory
= outer_func("hi")
my_func_1 = outer_func("bro")
my_func_2
my_func_1() my_func_2()
hi
bro
12.2 Basic Usage
def your_decorator(func):
def wrapper():
# Do stuff before func...
print("Before func!")
func()# Do stuff after func...
print("After func!")
return wrapper
12.2.1 Decorator
@your_decorator
def foo():
print("Hello World!")
foo()
Before func!
Hello World!
After func!
del foo # Clean up
12.2.2 Manually
def foo():
print("Hello World!")
= your_decorator(foo)
foo_dec foo_dec()
Before func!
Hello World!
After func!
12.3 Decorator with Parameters
def your_decorator(func):
def wrapper(*args,**kwargs):
# Do stuff before func...
print("Before func!")
*args,**kwargs)
func(# Do stuff after func...
print("After func!")
return wrapper
@your_decorator
def foo(bar):
print("My name is " + bar)
"Jack") foo(
Before func!
My name is Jack
After func!
12.4 Decorator Template
import functools
def your_decorator(func):
@functools.wraps(func) # For preserving the metadata of func.
def wrapper(*args,**kwargs):
# Do stuff before func...
= func(*args,**kwargs)
result # Do stuff after func..
return result
return wrapper