跳转到主要内容

cached_property() - 每个实例计算一次,作为属性缓存

项目描述

https://travis-ci.cn/penguinolog/backports.cached_property.svg?branch=master https://img.shields.io/pypi/v/backports.cached-property.svg https://img.shields.io/pypi/pyversions/backports.cached-property.svg https://img.shields.io/pypi/status/backports.cached-property.svg https://img.shields.io/github/license/penguinolog/backports.cached_property.svg https://img.shields.io/badge/code%20style-black-000000.svg

什么是

Python 3.8为functools添加了出色的描述符:cached_property。技术上,所有必需的API自Python 3.6以来都可用,但就是这样。

此包为Python 3.6和3.7回溯了此功能。

如何使用

from backports.cached_property import cached_property

然后Python 3.8文档将工作(因为代码更改很小)

将类的函数转换为属性,该属性值仅计算一次,然后作为正常属性缓存。类似于property,增加了缓存功能。对于其他情况下实际上是不可变的实例的昂贵计算属性很有用。

示例

class DataSet:
    def __init__(self, sequence_of_numbers):
        self._data = sequence_of_numbers

    @cached_property
    def stdev(self):
        return statistics.stdev(self._data)

    @cached_property
    def variance(self):
        return statistics.variance(self._data)

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源代码发行版

backports.cached-property-1.0.2.tar.gz (8.0 kB 查看哈希值)

上传时间 源代码

构建发行版

backports.cached_property-1.0.2-py3-none-any.whl (6.1 kB 查看哈希值)

上传时间 Python 3

由...