python進階路線,python進階之裝飾器之5把裝飾器作用到類和靜態方法上

 2023-11-19 阅读 25 评论 0

摘要:#學習本章知識,需要理解裝飾器的調用順序,理解@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(*a
#學習本章知識,需要理解裝飾器的調用順序,理解@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 rreturn wrapper# Class illustrating application of the decorator to different kinds of methods class Spam:@timethisdef instance_method(self, n):print(self, n)while n > 0:n -= 1@classmethod@timethisdef class_method(cls, n):print(cls, n)while n > 0:n -= 1@staticmethod@timethisdef 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

?

轉載于:https://www.cnblogs.com/max520liuhu/p/9350438.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/181388.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息