将装饰器定义为类

import types
from functools import wraps


class Profiled:
    def __init__(self, func):
        wraps(func)(self)
        self.ncalls = 0

    def __call__(self, *args, **kwargs):
        self.ncalls += 1
        return self.__wrapped__(*args, **kwargs)

    def __get__(self, instance, cls):
        if instance is None:
            return self
        else:
            return types.MethodType(self, instance)


@Profiled
def add(x, y):
    return x + y


class Spam:
    @Profiled
    def bar(self, x):
        print(self, x)


>>> print(add(2, 3))
5
>>> print(add(4, 5))
9
>>> print(add.ncalls)
2
>>> s = Spam()
>>> s.bar(1)
<__main__.Spam object at 0x109737510> 1
>>> s.bar(2)
<__main__.Spam object at 0x109737510> 2
>>> s.bar(3)
<__main__.Spam object at 0x109737510> 3
>>> print(Spam.bar.ncalls)
3

此处必须定义类对象的__call__()方法,因为在装饰器起作用时,实际上是Profiled的实例对象充当wrapper函数。实例对象要作为函数调用(使得其变成callable对象),必须实现__call__()方法。
此外,必须实现__get__()方法以使Spam的bar方法变成描述符对象,并通过types.MethodType进行方法绑定。

posted @ 2020-02-04 19:58  Jeffrey_Yang  阅读(322)  评论(0编辑  收藏  举报