# 你想给类方法或静态方法提供装饰器# 给类或静态方法提供装饰器是很简单的,不过要确保装饰器在 @classmethod 或 @staticmethod 之前。例如:import timefrom functools import wrapsdef timethis(func): @wraps(func) def wrapper(*args, **kwargs): start = time.time() r = func(*args, **kwargs) end = time.time() print(end - start) return r return wrapperclass 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 -= 1s = Spam()s.instance_method(10000000)Spam.class_method(10000000)Spam.static_method(10000000)
<__main__.Spam object at 0x0000014F7C5C9B70> 100000000.844519853591919<class '__main__.Spam'> 100000000.898040771484375100000000.8993453979492188