装饰器
装饰器(语法糖例子)
import time
def decorator(func):
def wrapper(*args, **kw):
print((time.time))
func(*args, **kw)
return wrapper
@decorator
def f1():
print('this is a function')
@decorator
def f2(func_name):
print('this is a function named ' + func_name)
@decorator
def f3(**kw):
print(kw)
f1()
f2('cc')
f3(a=1, b=2, c=3
对封装单元(如函数),不改变单元本身实现,通过装饰器改变行为其
代码复用
网友评论