() 方式 cti 调用 st2 rgs 显示 clas
1.以下代码,bar作为参数被test2调用。bar的原代码没变,但调用方式从bar()变成test2(bar) 不符合装饰器定义
- __author__ = "csy"
- def bar():
- print("in the bar")
- def test2(func):
- print(func)
- func()
- test2(bar)
输出:
in the bar
2.以下代码bar的原代码没变,调用方式仍为bar(),符合装饰器定义
- __author__ = "csy"
- def test2(func):
- def warpper(*args,**kwargs):
- print(func)
- func()
- return warpper
- @test2
- def bar():
- print("in the bar1")
- bar()
输出:
in the bar1
如果去掉 第9行 @test2,则只会显示
in the bar1
证明装饰器功能已实现
python装饰器(2)
来源: http://www.bubuko.com/infodetail-2288676.html