将装饰器定义为类

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进行方法绑定。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值