官术网_书友最值得收藏!

Cached properties

Each time we call a property, we are recalculating a function. If it is an expensive calculation, we might want to cache the result. This way, the next time the property is accessed, the cached value is returned:

from django.utils.functional import cached_property 
    #... 
    @cached_property 
    def full_name(self): 
        # Expensive operation e.g. external service call 
        return "{0} {1}".format(self.firstname, self.lastname) 

The cached value will be saved as a part of the Python instance in memory. As long as the instance exists, the same value will be returned.

As a fail-safe mechanism, you might want to force the execution of the Expensive operation to ensure that stale values are not returned. In such cases, set a keyword argument such as cached=False to prevent returning the cached value.

主站蜘蛛池模板: 青铜峡市| 虎林市| 南昌县| 磴口县| 四川省| 瓮安县| 夏河县| 延庆县| 疏勒县| 文化| 鹿泉市| 大城县| 乌苏市| 吉首市| 莱州市| 通州市| 塘沽区| 苍梧县| 三门县| 桐梓县| 武鸣县| 平舆县| 阳谷县| 冀州市| 保靖县| 镇沅| 全椒县| 客服| 雷波县| 涡阳县| 瑞丽市| 通化市| 华宁县| 张掖市| 黑龙江省| 中宁县| 包头市| 自治县| 肇庆市| 石楼县| 海城市|