美文网首页
装饰器的使用decorator

装饰器的使用decorator

作者: Future石 | 来源:发表于2018-01-15 16:53 被阅读0次

使用‘@’来使用decorator
'''

coding=utf-8

import time

def deco(func):
def wrapper():
starttime = time.time()
func()
endtime = time.time()
msecs = (endtime - starttime)*1000
print (">elapsed time: %f ms" % msecs)
return wrapper

@deco
def myfunc():
print 'starttime'
time.sleep(0.6)
print 'endtime'

print "myfunc is ",myfunc.name

myfunc()
'''
"@deco" 的本质就是"myfunc = deco(myfunc)"

相关文章

  • python Decorator (装饰器)

    Python 装饰器Python 装饰器 Decorator 使用kotlin的时候发现有个函数很好玩 measu...

  • Java23种设计模式之结构型模式「装饰器模式」

    装饰器模式 - Decorator Pattern 装饰器模式(Decorator Pattern)允许向一个现有...

  • ES6学习笔记(30)之 Decorator

    参考:ECMAScript 6 入门 Decorator还没有定案 什么是装饰器? 装饰器(Decorator)是...

  • Python中的Decorator装饰器

    Decorator 装饰器 理解decorator(装饰器)的关键, 在于理解在python中函数是第一公民, 并...

  • 装饰器

    代码运行期间动态增加功能的方式,称之为“装饰器”(Decorator) 装饰器的定义及使用 带参数的装饰器函数及使...

  • 装饰器的使用decorator

    使用‘@’来使用decorator''' coding=utf-8 import time def deco(fu...

  • Beautiful, Idiomatic Python (二)

    零、Decorator(@ Syntactic Sugar) 装饰器定义 A decorator is the n...

  • 装饰器

    装饰器 decorator类装饰器 带参数的装饰器 举例(装饰器函数;装饰器类;有参与无参) https://fo...

  • Python装饰器

    Python装饰器 装饰器的本质是什么? 装饰器等价于高阶函数,形如myfunc=decorator(myfunc...

  • python decorator

    实现一个log的decorator,使它既支持: 也支持: 实现: 使用log 装饰器装饰方法: 输出: @wra...

网友评论

      本文标题:装饰器的使用decorator

      本文链接:https://www.haomeiwen.com/subject/xdnsixtx.html