Lazy Attributes for Python Objects

1.6 · active · verified Tue Apr 14

The `lazy` library (version 1.6) provides a Python descriptor that allows object attributes to be computed only when they are first accessed, and then caches the result for subsequent retrievals. It enables efficient resource usage by deferring potentially expensive computations until absolutely necessary. The project maintains an active, though not rapid, release cadence, with the latest update in 2023.

Warnings

Install

Imports

Quickstart

Demonstrates the basic usage of the `@lazy` decorator on a method, turning it into a lazy-loaded, cached attribute. The computation ('computing bar') runs only on the first access.

from lazy import lazy

class Foo(object):
    @lazy
    def bar(self):
        print("computing bar")
        return 42

f = Foo()
print(f.bar)
print(f.bar)

view raw JSON →