代码
def logging(flag):
def fun_out(fn):
def inner(*args, **kwargs):
if flag=="+":
print("+")
elif flag == "-":
print("-")
rs = fn(*args, **kwargs)
return rs
return inner
return fun_out
@logging("+")
# --> = @ (logging("+")) logging是闭包,返回值为装饰器fun_out,flag='+'--> =@fun_out \\参数已经被闭包保存
def f1(a, b):
print(a+b)
@logging("-")
def f2(a, b):
print(a-b)
f1(1, 3)
f2(3, 4)
结果
+
4
-
-1