On this page

Many state isolation๏ƒ

๐ŸŸข Beginner ยท Lesson 29

"many" means ISOLATED - each melded instance owns its own state; mutating one never leaks into the next. The confusion this example prevents: expecting fresh objects to share anything.

Before you run๏ƒ

Use the Beginner guide for prerequisite concepts. Run from a checkout with Melder installed and Python 3.14 free-threading selected. The collection download includes the level's local helper modules.

Run the saved script๏ƒ

python UX_and_AIX_experiences/01_beginner/29_many_state_isolation.py
py -3.14t UX_and_AIX_experiences/01_beginner/29_many_state_isolation.py

Download this collection ยท Source on GitHub

Public surface๏ƒ

md.Existence "many" state isolation

Code๏ƒ

 1"""
 2TIER: beginner (29)
 3GOAL: "many" means ISOLATED - each melded instance owns its own state;
 4      mutating one never leaks into the next. The confusion this
 5      example prevents: expecting fresh objects to share anything.
 6SURFACE EXERCISED: md.Existence "many" state isolation
 7"""
 8import melder as md
 9
10
11class Basket:
12    def __init__(self) -> None:
13        self.items: list[str] = []
14
15
16def main() -> None:
17    book = md.Spellbook()
18    book.bind(spell=Basket, existence="many")
19    conduit = book.conjure()
20
21    first = conduit.meld(spell=Basket)
22    first.items.append("apples")
23    second = conduit.meld(spell=Basket)
24    assert first.items == ["apples"] and second.items == []
25    print("fresh instances, fresh state:", first.items, "vs", second.items)
26
27
28if __name__ == "__main__":
29    main()

Check the outcome๏ƒ

The script contains its own assertions or demonstrated refusal paths. Run it to evaluate those checks against your installed version. The code above is taken directly from the saved file; no run output is invented here.

More beginner examples ยท Level guide

API contracts๏ƒ