Leewoxinyiran 2020-05-04
装饰器本质也是一个函数, 只不过这个函数需要遵循以下规则:
(*args, **kwargs):
以满足所有函数需要
之后通过@语法糖即可装饰到任意函数上
# 不带参数的装饰器 def pre_do_sth(func): def wrapper(*args, **kwargs): print("Do sth before call one") func(*args, **kwargs) return wrapper @pre_do_sth def echo(msg): print(msg) echo("Hello World")
Do sth before call one Hello World
实际上调用的是 wrapper("Hello World") --> echo("Hello World")
只需要写一个返回 装饰器(入参只有一个, 返回值是一个函数)函数的函数
同样也能利用@语法糖
# 带参数的装饰器 def pre_do_sth_2(msg): def decorator(func): def wrapper(*args, **kwargs): print("Do sth before call two, print:%s"%(msg)) func(*args, **kwargs) return wrapper return decorator @pre_do_sth_2("Foo") def echo(msg): print(msg) echo("Hello World")
实际上@后面并不是对pre_do_sth_2这个函数生效 而是对pre_do_sth_2的返回值生效
Do sth before call two, print:Foo Hello World
先声明的装饰器先执行, 即在最外层
# 不带参数的装饰器 def pre_do_sth(func): def wrapper(*args, **kwargs): print("Do sth before call one") func(*args, **kwargs) return wrapper # 带参数的装饰器 def pre_do_sth_2(msg): def decorator(func): def wrapper(*args, **kwargs): print("Do sth before call two, print:%s"%(msg)) func(*args, **kwargs) return wrapper return decorator @pre_do_sth @pre_do_sth_2("Foo") def echo(msg): print(msg) echo("Hello World")
Do sth before call one Do sth before call two, print:Foo Hello World