On this page

Registration hooks๏ƒ

๐ŸŸก Intermediate ยท Lesson 03

Hooks ride the registration, not the call site - pre, activation, and post hooks attach through the fluent chain and fire around instance creation. The printed order IS the documentation; the 3.14t run records the exact firing points.

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/03_registration_hooks.py
py -3.14t UX_and_AIX_experiences/02_intermediate/03_registration_hooks.py

Download this collection ยท Source on GitHub

Public surface๏ƒ

md.SpellBinder.with_pre_hook / with_activation_hook / with_post_hook

Code๏ƒ

 1"""
 2TIER: intermediate (03)
 3GOAL: Hooks ride the registration, not the call site - pre, activation,
 4      and post hooks attach through the fluent chain and fire around
 5      instance creation. The printed order IS the documentation; the
 6      3.14t run records the exact firing points.
 7SURFACE EXERCISED: md.SpellBinder.with_pre_hook / with_activation_hook /
 8                   with_post_hook
 9"""
10import melder as md
11
12EVENTS: list[str] = []
13
14
15class Engine:
16    def __init__(self) -> None:
17        EVENTS.append("engine constructed")
18
19
20def note(stage: str):
21    def hook(*args: object, **kwargs: object) -> None:
22        EVENTS.append(stage)
23    return hook
24
25
26def main() -> None:
27    book = md.Spellbook()
28    md.SpellBinder(book).bind(Engine) \
29        .as_unique() \
30        .with_pre_hook(note("pre")) \
31        .with_activation_hook(note("activation")) \
32        .with_post_hook(note("post")) \
33        .finalize()
34
35    conduit = book.conjure()
36    engine = conduit.meld(spell=Engine)
37    assert isinstance(engine, Engine)
38    assert "engine constructed" in EVENTS
39    print("lifecycle event order:", " -> ".join(EVENTS))
40
41
42if __name__ == "__main__":
43    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

API contracts๏ƒ