装饰器类学习小结

发布时间:2020-06-01 15:48:12 作者:c303112495
来源:网络 阅读:411

装饰器

装饰器的原理以及函数类型的装饰器在网上有很多描述,本文我就只讲我对于 将装饰器定义为类的理解。

要将装饰器定义为一个类,需要在类中声明__call____get__方法,例子如下:

from time import time

class ttl_property(object):
    def __init__(self, ttl=None):
       self.ttl = ttl

    def __call__(self, func):
       def wrapper(*args,**kw):
          if 'name' not in self.__dict__.keys():
             self.__dict__['name']=(func(*args,**kw),time())
          last=self.__dict__['name'][1]
          value=self.__dict__['name'][0]
          now=time()
          if now-last>self.ttl:
             value=func(*args,**kw)
             self.__dict__['name']=(value,now)
          return value
       return wrapper

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

    def __set__(self, instance, value):
       self.__dict__['name'] = (value, time())
from ttl_property import ttl_property

class Book(object):
    """
    >>> b = Book()
    >>> b.price
    80.0
    >>> b.price
    80.0
    >>> time.sleep(3)
    >>> b.price
    64.0
    >>> b.price
    64.0
    >>> time.sleep(3)
    >>> b.price
    51.2
    """

    def __init__(self):
        self._price = 100.0

    @ttl_property(ttl=2)
    def price(self):
        self._price = self._price * 0.8
        return self._price

这是我在一个网站上做的实验,在这个实验中需要定义一个装饰器类ttl_property来装饰Book类中的函数,__call__函数可以将类的调用和函数类似,具体请查询网上资料。

我要着重强调两点:

1:装饰器类中的__get__方法很重要,因为在装饰器中返回的函数并不是原本类中的函数,也就是说在原本类所对应的实例中,这个函数并不存在,所以如果没有__get__方法,那么调用就会出问题;那么types.MethodType(self, instance)就是将方法和实例绑定起来,这样在这个实例中就包含了这个方法,就可以顺利调用了。

2:如果在原来的方法中需要使用self,那么在装饰器返回的方法中也要包含self参数,不然就不行

推荐阅读:
  1. 函数装饰器和类装饰器的使用方法
  2. Python学习—装饰器

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python 装饰器 学习小结

上一篇:dns是什么?

下一篇:哈希如何帮助存储和检索HashMap中的值?

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》