Python, 这一个缓存装饰器, 其执行流程是怎样的?
大家讲道理
大家讲道理 2017-04-18 10:18:42
[Python讨论组]

2017/2/6

描述

比如, 考虑这样一段代码, 它的执行流程是怎样的呢 ?

class Foo(object):
    @cached_property
    def foo(self):
        # calculate something important here
        return 42
        
f = Foo()
f.foo

f.foo   

相关代码

  • 以class为基础的缓存装饰器

class cached_property(property):

    """A decorator that converts a function into a lazy property.  The
    function wrapped is called the first time to retrieve the result
    and then that calculated result is used the next time you access
    the value::
        class Foo(object):
            @cached_property
            def foo(self):
                # calculate something important here
                return 42
    The class has to have a `__dict__` in order for this property to
    work.
    """

    # implementation detail: A subclass of python's builtin property
    # decorator, we override __get__ to check for a cached value. If one
    # choses to invoke __get__ by hand the property will still work as
    # expected because the lookup logic is replicated in __get__ for
    # manual invocation.

    def __init__(self, func, name=None, doc=None):
        self.__name__ = name or func.__name__
        self.__module__ = func.__module__
        self.__doc__ = doc or func.__doc__
        self.func = func

    def __set__(self, obj, value):
        obj.__dict__[self.__name__] = value

    def __get__(self, obj, type=None):
        if obj is None:
            return self
        value = obj.__dict__.get(self.__name__, _missing)
        if value is _missing:
            value = self.func(obj)
            obj.__dict__[self.__name__] = value
        return value

上下文环境

  • 产品版本: Python2

  • 操作系统: Linux

搜索

  • 相似的问题: http://stackoverflow.com/ques...

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(2)
巴扎黑

cached_propertyproperty 的subclass, 复写了 __get__, __set__ 方法.

cached_property 是一个描述器(资料描述器),获取属性的时候优先从描述器获取,即(__get__).

所以执行流程就是:
f.foo -> __get__ -> 从实例字典(f.__dict__)获取 -> 如果没有则保存到字典并调用实际方法返回

阿神
def cached_property(func):
    def _deco(*args, **kwargs):
        print(22222222222222)
        ret = func(*args, **kwargs) #这是调用foo方法
        print(44444444444444)
        return ret
    return _deco
class Foo(object):
    def __init__(self):
        print (111111111111111111)
    @cached_property
    def foo(self):
        print(3333333333333)
        return 42

f = Foo()
f.foo()
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号