码迷,mamicode.com
首页 > 编程语言 > 详细

python :装饰器

时间:2016-10-14 00:24:58      阅读:163      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!