On this page

Disposal contract๏ƒ

๐ŸŸข Beginner ยท Lesson 08

Teardown is part of registration - disposal_method_names tells melder which methods to call on YOUR instances when their owner cleans up. No base class, no protocol: name the method, done. The printed stages document exactly when disposal fires.

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

Download this collection ยท Source on GitHub

Public surface๏ƒ

bind(disposal_method_names=[...]), cleanup cascade

Code๏ƒ

 1"""
 2TIER: beginner (08)
 3GOAL: Teardown is part of registration - disposal_method_names tells
 4      melder which methods to call on YOUR instances when their owner
 5      cleans up. No base class, no protocol: name the method, done.
 6      The printed stages document exactly when disposal fires.
 7SURFACE EXERCISED: bind(disposal_method_names=[...]), cleanup cascade
 8"""
 9import melder as md
10
11CLOSED: list[str] = []
12
13
14class PooledConnection:
15    def close(self) -> None:
16        CLOSED.append("connection closed")
17
18
19def main() -> None:
20    book = md.Spellbook()
21    book.bind(
22        spell=PooledConnection,
23        existence="unique",
24        disposal_method_names=["close"],
25    )
26    conduit = book.conjure()
27    conn = conduit.meld("PooledConnection")
28    assert isinstance(conn, PooledConnection) and not CLOSED
29
30    conduit.cleanup()
31    print("after conduit.cleanup():", CLOSED or "not yet disposed")
32    book.cleanup()
33    print("after book.cleanup():", CLOSED or "not disposed")
34    assert CLOSED, "disposal_method_names contract: close() must fire on teardown"
35    print("disposal contract held:", CLOSED[0])
36
37
38if __name__ == "__main__":
39    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