Skip to content

Python 3.8: Cached property  #67

@felix-andreas

Description

@felix-andreas

Python 3.8 now has a functools.cached_property

from functools import cached_property

class Foo:
    def __init__(self):
        self._x = 0

    @cached_property
    def x(self):
        print("Increment x!")
        return self._x

Create a new foo:

>>> foo = Foo()
>>> foo.x
Increment x!
1
>>> foo.x
1

The cache can be invalidated with:

>>> del foo.x
>>> foo.x
Increment x!
2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions