首页 > 编程语言 > 详细

python :装饰器

时间:2016-10-14 00:22:01      阅读:264      评论:0      收藏:0      [点我收藏+]
装饰器:本质是一个函数,就是为其他函数添加附件功能
特性:1.不能修改被装饰函数的源代码
2.不能修改被装饰函数的调用方式
#装饰器
import time
def deco(func):
    def gao(*args,**kwargs):
        start_time=time.time()
        func(*args,**kwargs)
        stop_time=time.time()
        print("in the func %s" %(stop_time-start_time))
    return gao
@deco
def test():
    time.sleep(1)
    print("in the test")
def test2(name): #test2=deco(test2)=gao
    time.sleep(1)
    print("in the test2",name)
test()
test2("alex")

 

python :装饰器

原文:http://www.cnblogs.com/xuehuahongmei/p/5958480.html

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