码迷,mamicode.com
首页 > 其他好文 > 详细

装饰器1(被装饰函数不带参数)

时间:2017-10-01 13:37:32      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:pytho   bsp   run   执行   test   stop   wrap   time   地址   

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import time

def wrapper(func):
    def deco():
        start = time.time()
        func()
        stop = time.time()
        print("the func run %s"%(stop - start))
    return deco

#这里的【@wrapper】等于【test1 = wrapper(test1)】,wrapper(test1),将test1函数以实参的形式传递给wrapper函数,wrapper函数将会把deco函数的内存地址作为返回值返回给变量test1,而此时test1变量所指引的值相当于是deco函数的内存地址,如果test1()则将会执行deco函数
@wrapper  
def test1():
    time.sleep(3)    
    print("in the test1...")
@wrapper
def test2():
    time.sleep(3)    
    print("in the test2...")
test1()
test2()

 

装饰器1(被装饰函数不带参数)

标签:pytho   bsp   run   执行   test   stop   wrap   time   地址   

原文地址:http://www.cnblogs.com/sunweigogogo/p/7617048.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!