Skip to main content

cached_property() - computed once per instance, cached as attribute

Project description

https://travis-ci.com/penguinolog/backports.cached-property.svg?branch=master https://dev.azure.com/penguinolog/backports.cached-property/_apis/build/status/python-useful-helpers.exec-helpers?branchName=master https://img.shields.io/pypi/v/exec-helpers.svg https://img.shields.io/pypi/pyversions/exec-helpers.svg https://img.shields.io/pypi/status/exec-helpers.svg https://img.shields.io/github/license/penguinolog/backports.cached-property.svg https://img.shields.io/badge/code%20style-black-000000.svg

What

Python 3.8 adds great descriptor to functools: cached_property. Technically all required APIs was available since python 3.6, but it is what it is.

This package is a backport of this functionality for python 3.6 and 3.7.

How to use

from backports.cached_property import cached_property

And then python 3.8 documentation will work (because code is minimally changed):

Transform a method of a class into a property whose value is computed once and then cached as a normal attribute for the life of the instance. Similar to property, with the addition of caching. Useful for expensive computed properties of instances that are otherwise effectively immutable.

Example:

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

backports.cached-property-1.0.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file backports.cached-property-1.0.0.tar.gz.

File metadata

  • Download URL: backports.cached-property-1.0.0.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.6

File hashes

Hashes for backports.cached-property-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a7a390f4c006db13a3eb268f79dc0fd435bd0739bb570468a90979ef7b1bfce2
MD5 4fcaef707ecb52a6801616bdf64499f6
BLAKE2b-256 1ee683588363002c8070f9f2a83e7ab77585a6f4736233ad7d2cd8e356a00017

See more details on using hashes here.

File details

Details for the file backports.cached_property-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: backports.cached_property-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.6

File hashes

Hashes for backports.cached_property-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bbd57fe651467abf04b3791d9bf6f56444e6f31c0dac9fac8070728b105ebe7f
MD5 9c2b67675c74c549bd072f912edc0576
BLAKE2b-256 901376ae96131a7f9128b7f79308916d0500e3744f565a08a71846cae52a721b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page