Lazify

0.4.0 · maintenance · verified Tue Apr 14

Lazify aims to be the defacto standard and most complete library for Python tools related to lazy evaluation. It is designed for deferred computation, evaluating expressions or attributes only when they are first accessed. The current version is 0.4.0, released in 2018, indicating a lack of active maintenance and a very slow release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `LazyProxy` for general lazy evaluation and the `@lazy` decorator for lazy attributes within classes. The decorated method or proxied function is only executed upon its first access, and its result is then cached for subsequent calls.

from lazify import LazyProxy, lazy

# Example 1: LazyProxy for deferred expression evaluation
def expensive_calculation():
    print("  [TRACE] Performing expensive calculation inside function...")
    return 42

print("Creating LazyProxy object...")
lazy_value = LazyProxy(expensive_calculation)
print("LazyProxy created, but calculation not yet performed.")

print(f"First access to lazy_value: {lazy_value}") # This triggers the calculation
print(f"Second access to lazy_value: {lazy_value}") # This uses the cached result

print("\n" + "-" * 20 + "\n")

# Example 2: @lazy decorator for lazy attributes in a class
class MyClass:
    def __init__(self, initial_value):
        self._initial_value = initial_value

    @lazy
    def processed_value(self):
        print(f"  [TRACE] Processing value {self._initial_value} with decorator...")
        return self._initial_value * 2

obj = MyClass(10)
print("MyClass instance created, 'processed_value' not yet computed.")

print(f"First access to obj.processed_value: {obj.processed_value}") # This triggers the processing
print(f"Second access to obj.processed_value: {obj.processed_value}") # This uses the cached result

view raw JSON →