python-immutable-accumulator

Create immutable Python accumulators using frozen dataclasses and tuples.

Updated Aug 27, 2026
One-click install
npx skills add https://github.com/shimo4228/claude-code-learned-skills --skill python-immutable-accumulator
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-immutable-accumulator
Source: https://github.com/shimo4228/claude-code-learned-skills/tree/main/skills/python-immutable-accumulator
Command: npx skills add https://github.com/shimo4228/claude-code-learned-skills --skill python-immutable-accumulator

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Mutable state accumulation is a common source of bugs, making debugging, reasoning about code, and concurrent execution unpredictable. This pattern uses a frozen dataclass with slots and a tuple to store records so that every update yields a new, immutable instance, eliminating shared mutable state and enabling safer reasoning about state changes.

Core Features & Use Cases

  • Core Pattern: immutable accumulation by returning a new instance from add(), enabled by frozen=True and slots=True dataclasses.
  • Safe totals and iteration: lightweight, hashable containers support straightforward aggregation and traversal.
  • Pydantic Variant: optional model-based variant for environments that rely on Pydantic for validation.
  • Thread-safe and functional style: natural fit for pipelines and parallel processing without side effects.
  • Practical guidance: designed for use in cost tracking, event sourcing, and stateful workflows requiring reproducibility.

Quick Start

Create an empty Accumulator and repeatedly call add() with Record instances to build an immutable sequence.

Frequently Asked Questions about python-immutable-accumulator

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement immutable state management in Python?

You can implement immutable state management using frozen dataclasses with slots and tuple-based storage, where methods return new instances. This pattern eliminates shared mutable state and ensures safer state changes.

What is the best way to track accumulated state without side effects in Python?

Using a frozen dataclass accumulator that returns a new instance from each add() operation is the best way to track state without side effects. This functional pattern naturally enables thread safety and parallel processing.

Does Python support event sourcing with immutable dataclasses?

Yes, Python supports event sourcing using frozen dataclasses with slots and tuple-based storage. Each add() operation yields a new immutable instance, enabling reproducible stateful workflows and functional pipelines.

Can I use Pydantic for immutable state accumulation in Python?

Yes, an optional Pydantic variant provides immutable state accumulation for environments relying on Pydantic validation. It uses the same pattern of returning new instances from add operations while integrating model-based validation.

Do I need Python 3.8 to use frozen dataclasses with slots for state management?

Yes, Python 3.8 or later is required because the immutable accumulator pattern relies on the slots=True parameter alongside frozen=True in dataclasses. This ensures tuple-based storage and new instance creation work correctly.

Why should I use an immutable accumulator instead of mutable state for cost tracking?

You should use an immutable accumulator for cost tracking because mutable state accumulation causes bugs and unpredictable concurrent execution. Returning new instances from every update eliminates shared state, enabling safer debugging and functional pipelines.