On this page

Same class many names๏ƒ

๐ŸŸข Beginner ยท Lesson 24

One class, one spell - a spell_id is the SHA256 of the class's code, so the SAME class registers once, whatever binding name or lifecycle you give it (beginner 30). The working pattern for one shape in many roles: tiny subclasses, one per role - each is a new class, so its own spell, at its own address. (Same-NAMED classes with different code may also coexist at different addresses since 0.2.8206; the contract probes pin both rules.)

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

Download this collection ยท Source on GitHub

Public surface๏ƒ

subclass-per-role registration, mixed lifecycles

Code๏ƒ

 1"""
 2TIER: beginner (24)
 3GOAL: One class, one spell - a spell_id is the SHA256 of the class's
 4      code, so the SAME class registers once, whatever binding name or
 5      lifecycle you give it (beginner 30). The working pattern for one
 6      shape in many roles: tiny subclasses, one per role - each is a new
 7      class, so its own spell, at its own address. (Same-NAMED classes
 8      with different code may also coexist at different addresses since
 9      0.2.8206; the contract probes pin both rules.)
10SURFACE EXERCISED: subclass-per-role registration, mixed lifecycles
11"""
12import melder as md
13
14
15class Buffer:
16    pass
17
18
19class SharedBuffer(Buffer):
20    pass
21
22
23class ScratchBuffer(Buffer):
24    pass
25
26
27def main() -> None:
28    book = md.Spellbook()
29    book.bind(spell=SharedBuffer, existence="unique",
30              spellframe="pools", binding_name="shared")
31    book.bind(spell=ScratchBuffer, existence="many",
32              spellframe="pools", binding_name="scratch")
33    conduit = book.conjure()
34
35    shared_a = conduit.meld(spellframe="pools", binding_name="shared")
36    shared_b = conduit.meld(spellframe="pools", binding_name="shared")
37    scratch_a = conduit.meld(spellframe="pools", binding_name="scratch")
38    scratch_b = conduit.meld(spellframe="pools", binding_name="scratch")
39    assert shared_a is shared_b
40    assert scratch_a is not scratch_b
41    assert isinstance(shared_a, Buffer) and isinstance(scratch_a, Buffer)
42    print("one shape, two roles via subclasses: shared + scratch")
43
44
45if __name__ == "__main__":
46    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