zoukankan      html  css  js  c++  java
  • python进阶之装饰器之5把装饰器作用到类和静态方法上

    #学习本章知识,需要理解装饰器的调用顺序,理解@classmethod @staticmethod

    import
    time from functools import wraps # A simple decorator def timethis(func): @wraps(func) def wrapper(*args, **kwargs): start = time.time() r = func(*args, **kwargs) end = time.time() print(end-start) return r return wrapper # Class illustrating application of the decorator to different kinds of methods class Spam: @timethis def instance_method(self, n): print(self, n) while n > 0: n -= 1 @classmethod @timethis def class_method(cls, n): print(cls, n) while n > 0: n -= 1 @staticmethod @timethis def static_method(n): print(n) while n > 0: n -= 1 s = Spam() print(s.instance_method(1000000)) # <__main__.Spam object at 0x1006a6050> 1000000 # 0.11817407608032227 print(Spam.class_method(1000000)) # <class '__main__.Spam'> 1000000 # 0.11334395408630371 print(Spam.static_method(1000000)) # 1000000 # 0.11740279197692871
    仙衣眠云碧岚袍,一襟潇洒,两袖飘飘。玉墨舒心春酝瓢,行也逍遥,坐也逍遥。
  • 相关阅读:
    HDU4611+数学
    HDU4612+Tarjan缩点+BFS求树的直径
    HDU4602+推导公式
    HDU4607+BFS
    HDU1353+贪心
    HDU4545+LCS
    HDU4548+素数
    HDU4539+状态压缩DP
    HDU2110+母函数
    HDU1569+最大点权集
  • 原文地址:https://www.cnblogs.com/max520liuhu/p/9350438.html
Copyright © 2011-2022 走看看