首页 > 编程语言 > 详细

python装饰器(2)

时间:2017-09-02 18:54:47      阅读:250      评论:0      收藏:0      [点我收藏+]

 

1.以下代码,bar作为参数被test2调用。bar的原代码没变,但调用方式从bar()变成test2(bar) 不符合装饰器定义

 1 __author__ = "csy"
 2 
 3 def bar():
 4     print("in the bar")
 5 
 6 def test2(func):
 7     print(func)
 8     func()
 9 
10 test2(bar)

输出:

<function bar at 0x00000000006BFE18>
in the bar

 

2.以下代码bar的原代码没变,调用方式仍为bar(),符合装饰器定义

 1 __author__ = "csy"
 2 
 3 def test2(func):
 4     def warpper(*args,**kwargs):
 5         print(func)
 6         func()
 7     return warpper
 8 
 9 @test2
10 def bar():
11     print("in the bar1")
12 
13 bar()

输出:

<function bar at 0x0000000000A4FEA0>
in the bar1

如果去掉 第9行 @test2,则只会显示

in the bar1

证明装饰器功能已实现

python装饰器(2)

原文:http://www.cnblogs.com/csy113/p/7467194.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!