Property Manager

3.0 · active · verified Wed Apr 15

The `property-manager` package, currently at version 3.0, provides useful property variants for Python programming, extending Python's built-in `property` descriptor. It offers decorators for creating required properties, writable properties, and cached properties, as well as a `PropertyManager` base class with enhanced constructor behavior and `repr()` customization. The library has a stable release cadence, with version 3.0 released in 2020, and is designed to make managing class attributes more robust and flexible.

Warnings

Install

Imports

Quickstart

This example demonstrates defining a class using `PropertyManager` with `required_property`, `writable_property`, and `cached_property`. It shows how required properties are enforced at instantiation, how writable properties can have computed defaults and be assigned new values, and how cached properties compute their value only once upon first access.

from property_manager import PropertyManager, required_property, writable_property, cached_property
import random

class MyManagedObject(PropertyManager):
    @required_property
    def name(self):
        """A name that must be provided during initialization."""
        pass

    @writable_property
    def value(self):
        """A writable property with a computed default."""
        return random.randint(1, 100)

    @cached_property
    def expensive_calculation(self):
        """A property whose value is computed once and cached."""
        print("Calculating expensive_calculation...")
        return sum(range(self.value * 1000))

# Instantiate with a required property
obj = MyManagedObject(name="Example Item")

# Access writable property (gets default initially)
print(f"Initial value: {obj.value}")

# Set writable property
obj.value = 200
print(f"New value: {obj.value}")

# Access cached property (computes once)
print(f"Expensive calculation (first access): {obj.expensive_calculation}")
print(f"Expensive calculation (second access): {obj.expensive_calculation}")

# Attempt to create an object without a required property
try:
    MyManagedObject()
except TypeError as e:
    print(f"Caught expected error: {e}")

view raw JSON →