栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python装饰器,自我混淆

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python装饰器,自我混淆

像这样使用描述符协议:

import functoolsclass cacher(object):    def __init__(self, f):        self.f = f        self.cache = {}    def __call__(self, *args):        fname = self.f.__name__        if (fname not in self.cache): self.cache[fname] = self.f(self,*args)        else: print "using cache"        return self.cache[fname]    def __get__(self, instance, instancetype):        """Implement the descriptor protocol to make decorating instance         method possible.        """        # Return a partial function with the first argument is the instance         #   of the class decorated.        return functools.partial(self.__call__, instance)

编辑:

如何运作?

在装饰器中使用描述符协议将使我们能够以正确的实例作为自身来访问装饰的方法,也许一些代码可以提供更好的帮助:

现在,当我们要做的时候:

class Session(p.Session):    ...    @cacher    def get_something(self):        print "get_something called with self = %s "% self        return self.pl.get_something()

相当于:

class Session(p.Session):    ...    def get_something(self):        print "get_something called with self = %s "% self        return self.pl.get_something()    get_something = cacher(get_something)

因此,现在get_something是cacher的实例。因此,当我们调用方法get_something时,它将被转换为此(由于描述符协议):

session = Session()session.get_something  #  <==> session.get_something.__get__(get_something, session, <type ..>)# N.B: get_something is an instance of cacher class.

并且因为:

session.get_something.__get__(get_something, session, <type ..>)# returnget_something.__call__(session, ...) # the partial function.

所以

session.get_something(*args)# <==>get_something.__call__(session, *args)

希望这会解释它是如何工作的:)



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/639057.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号