On this page
Bind functions and instances๏
๐ข Beginner ยท Lesson 03
Spells are not only classes - functions and ready-made objects bind with the same verb. RUNTIME LAW (proven by the harness): callable and pre-built spells are always "unique" - the factory runs once and its product is shared; instances are already built.
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/03_bind_functions_and_instances.py
py -3.14t UX_and_AIX_experiences/01_beginner/03_bind_functions_and_instances.py
Public surface๏
function spells, instance spells, string vocabulary
Code๏
1"""
2TIER: beginner (03)
3GOAL: Spells are not only classes - functions and ready-made objects
4 bind with the same verb. RUNTIME LAW (proven by the harness):
5 callable and pre-built spells are always "unique" - the factory
6 runs once and its product is shared; instances are already built.
7SURFACE EXERCISED: function spells, instance spells, string vocabulary
8"""
9import melder as md
10
11
12def make_settings() -> dict:
13 return {"region": "us-east", "retries": 3}
14
15
16class AlreadyBuilt:
17 def __init__(self, label: str) -> None:
18 self.label = label
19
20
21def main() -> None:
22 book = md.Spellbook()
23 book.bind(spell=make_settings, existence="unique")
24
25 prebuilt = AlreadyBuilt("built-by-hand")
26 book.bind(spell=prebuilt, existence="unique")
27
28 conduit = book.conjure()
29
30 settings = conduit.meld(spell=make_settings)
31 print("function spell melded ->", settings)
32 again = conduit.meld(spell=make_settings)
33 print("unique law: same product back?", settings is again)
34
35 held = conduit.meld(spell=prebuilt)
36 assert held is prebuilt and held.label == "built-by-hand"
37 print("instance spell melded:", held.label)
38
39
40if __name__ == "__main__":
41 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.