recordclass

0.24 · active · verified Thu Apr 16

recordclass is a Python library that provides mutable variants of `collections.namedtuple`, supporting assignments and offering memory-saving alternatives like `dataobject` and `structclass`. These types aim for high performance and reduced memory footprint by optionally disabling cyclic garbage collection and removing instance dictionaries. Currently at version 0.24, the library is actively maintained with releases supporting the latest Python versions, including 3.14, and aims to be a fast, memory-efficient, and flexible choice for data representation.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create mutable record-like objects using `recordclass`, `dataobject` (similar to dataclasses), `make_dataclass`, and the `as_record` decorator. It highlights the mutability aspect and attribute access.

from recordclass import recordclass, dataobject, make_dataclass, as_record
import os

# Using recordclass (mutable namedtuple-like)
Point = recordclass('Point', 'x y')
p = Point(1, 2)
print(f"Initial Point: {p}")
p.x = 10
print(f"Modified Point: {p}")

# Using dataobject (compact dataclass-like)
class ColorPoint(dataobject):
    x: int
    y: int
    color: str = 'red'

cp = ColorPoint(1, 2)
print(f"ColorPoint: {cp}")
cp.color = 'blue'
print(f"Modified ColorPoint: {cp}")

# Using make_dataclass
User = make_dataclass("User", [("name", str), ("age", int)])
u = User("Alice", 30)
print(f"User: {u}")

# Using as_record decorator
@as_record()
def Product(name: str, price: float, sku=None):
    pass

prod = Product("Laptop", 1200.0, "LAP001")
print(f"Product: {prod}")

view raw JSON →