主要看了下面这位大佬写的,对python的装饰器有了理解
https://blog.youkuaiyun.com/xiangxianghehe/article/details/77170585
下面这篇也很有参考价值,做了更加详细的解释
https://blog.youkuaiyun.com/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
本文通过解析两篇高质量博客,详细介绍了Python装饰器的工作原理及其使用方法。文章提供了多个实例,包括一个万能装饰器的实现,展示了装饰器如何增强或修改函数行为,对初学者和进阶开发者均有很大帮助。

被折叠的 条评论
为什么被折叠?



