装饰器作用:
比如下面一个函数:
def my_hello():
print('Hello World!')
my_hello()
现在有一个要求,就是在打印 Hello World! 之前先打印 WTF!
- 装饰器:
def my_decorate(func):
def inner():
print('WTF')
func()
return inner
@my_decorate
def my_hello():
print('Hello World!')
my_hello()
好处:解决代码重用,易于修改
当函数带有参数时:
def my_decorate(func):
def inner(*args, **kwargs):
print('WTF')
func(*args, **kwargs)
return inner
@my_decorate
def my_hello(str):
print('Hello World! %s' % str)
my_hello('HaHaHa')
当函数带有返回值时:
def my_decorate(func):
def inner(*args, **kwargs):
print('WTF')
return func(*args, **kwargs)
return inner
@my_decorate
def my_hello(str, a, b):
print('Hello World! %s' % str)
return a + b
print(my_hello('HaHaHa', 1, 2)) # 当装饰器中没有 return func(*args, **kwargs) 的返回值时,会打印 None ,即空
# 但是当打印函数私有属性 __name__ 时,会发现其变成了装饰器返回的函数名,即 inner
print(my_hello.__name__)
解决装饰器链式调用重名问题:
from functools import wraps
def my_decorate(func):
@wraps(func)
def inner(*args, **kwargs):
print('WTF')
return func(*args, **kwargs)
return inner
@my_decorate
def my_hello(str, a, b):
print('Hello World! %s' % str)
return a + b
my_hello('HaHaHa', 1, 2)
print(my_hello.__name__)