python装饰器就是用于拓展原来函数功能的一种函数,这个函数的特殊之处在于它的返回值也是一个函数,使用python装饰器的好处就是在不用更改原函数的代码前提下给函数增加新的功能。
def test(a_func):
def test_old():
print("I am 开始")
a_func()
print("I am 结束")
return test_old
@test
def test1():
print("I am 插入的功能")
test1()
1、test函数是原有功能,但是又是核心功能,不能动里面的代码;
2、如果给test函数增加新功能,需要去重新写一个函数test1,然后用test函数区装饰test1函数;
3、test1函数的执行结果:
I am 开始
I am 插入的功能
I am 结束
以上就是最简单的装饰器的使用
原文:https://www.cnblogs.com/pizicheng/p/14549776.html