主要看了下面这位大佬写的,对python的装饰器有了理解
https://blog.csdn.net/xiangxianghehe/article/details/77170585
下面这篇也很有参考价值,做了更加详细的解释
https://blog.csdn.net/u010358168/article/details/77773199
万能修饰器:
def w_test(func):
def inner(*args, **kwargs):
ret = func(*args, **kwargs)
return ret
return inner
@w_test
def test():
print('test called')
@w_test
def test1():
print('test1 called')
return 'python'
@w_test
def test2(a):
print('test2 called and value is %d ' % a)
test()
test1()
test2(9)
运行结果为:
test called
test1 called
test2 called and value is 9