On this page

Bind lambda spells๏ƒ

๐ŸŸก Intermediate ยท Lesson 12

Even a lambda is a spell - it MUST carry a binding_name (the law the runtime enforces); melds address it by (frame, name) because binding_name alone is never an address. Callable = unique.

Before you run๏ƒ

Use the Intermediate 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/02_intermediate/12_bind_lambda_spells.py
py -3.14t UX_and_AIX_experiences/02_intermediate/12_bind_lambda_spells.py

Download this collection ยท Source on GitHub

Public surface๏ƒ

bind(spell=), binding_name, callable-unique law

Code๏ƒ

 1"""
 2TIER: intermediate (12)
 3GOAL: Even a lambda is a spell - it MUST carry a binding_name (the law
 4      the runtime enforces); melds address it by (frame, name) because
 5      binding_name alone is never an address. Callable = unique.
 6SURFACE EXERCISED: bind(spell=<lambda>), binding_name, callable-unique law
 7"""
 8import melder as md
 9
10
11def main() -> None:
12    book = md.Spellbook()
13    book.bind(
14        spell=lambda: {"feature_flags": {"dark_mode": True}},
15        existence="unique",
16        spellframe="flags",
17        binding_name="flag-factory",
18    )
19    conduit = book.conjure()
20    flags_a = conduit.meld(spellframe="flags", binding_name="flag-factory")
21    flags_b = conduit.meld(spellframe="flags", binding_name="flag-factory")
22    print("lambda product:", flags_a)
23
24    # The lambda RAN, and what you get back is its product - not the
25    # lambda itself. A callable spell is invoked to produce the object.
26    assert flags_a == {"feature_flags": {"dark_mode": True}}
27    assert not callable(flags_a), "you meld the PRODUCT, not the callable"
28
29    # CALLABLE = UNIQUE. Both melds returned the SAME dict, so the lambda
30    # was called once and its product shared. Were this `many`, the second
31    # meld would have re-run the lambda and produced an equal-but-separate
32    # dict - equality would hold and identity would not.
33    assert flags_a is flags_b
34    print("unique law: shared product?", flags_a is flags_b)
35    print("  same object, so the lambda ran ONCE - `many` would have")
36    print("  produced two equal dicts that are not the same object")
37
38    # ADDRESSING IS (frame, name), AND binding_name IS NOT AN ENTRY MODE.
39    # meld has four ways in - a spell_id string, a spell object, a
40    # spellframe, or a spell_name. `binding_name` is none of them: it is a
41    # binding KEY used during resolution, so on its own there is nothing
42    # to resolve FROM and the call refuses with ValueError.
43    try:
44        conduit.meld(binding_name="flag-factory")
45        raise AssertionError("binding_name alone must not be an address")
46    except ValueError as unaddressed:
47        print("meld(binding_name=...) alone -> ValueError:",
48              str(unaddressed)[:70])
49        print("  that is why the working call above passes spellframe TOO")
50
51
52if __name__ == "__main__":
53    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 intermediate examples ยท Level guide