On this page

Lifecycles unique vs many๏ƒ

๐ŸŸข Beginner ยท Lesson 02

The one decision every binding makes - where does instance reuse stop? unique = one shared instance; many = fresh construction per meld. This is Existence, the heart of the bind vocabulary.

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/02_lifecycles_unique_vs_many.py
py -3.14t UX_and_AIX_experiences/01_beginner/02_lifecycles_unique_vs_many.py

Download this collection ยท Source on GitHub

Public surface๏ƒ

md.Spellbook, existence="unique" / "many"

Code๏ƒ

 1"""
 2TIER: beginner (02)
 3GOAL: The one decision every binding makes - where does instance reuse
 4      stop? unique = one shared instance; many = fresh construction per
 5      meld. This is Existence, the heart of the bind vocabulary.
 6SURFACE EXERCISED: md.Spellbook, existence="unique" / "many"
 7"""
 8import melder as md
 9
10
11class SharedCache:
12    pass
13
14
15class RequestScratchpad:
16    pass
17
18
19def main() -> None:
20    book = md.Spellbook()
21    book.bind(spell=SharedCache, existence="unique")
22    book.bind(spell=RequestScratchpad, existence="many")
23    conduit = book.conjure()
24
25    cache_a = conduit.meld(spell=SharedCache)
26    cache_b = conduit.meld(spell=SharedCache)
27    assert cache_a is cache_b
28    print("unique: one instance, shared -", cache_a is cache_b)
29
30    pad_a = conduit.meld(spell=RequestScratchpad)
31    pad_b = conduit.meld(spell=RequestScratchpad)
32    assert pad_a is not pad_b
33    print("many: fresh instance per meld -", pad_a is not pad_b)
34
35
36if __name__ == "__main__":
37    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๏ƒ