On this page

Disposal multiple methods๏ƒ

๐ŸŸข Beginner ยท Lesson 35

disposal_method_names takes a LIST - complex resources name every teardown verb they need, and the printed order documents how the runtime walks them.

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

Download this collection ยท Source on GitHub

Public surface๏ƒ

bind(disposal_method_names=["flush", "close"])

Code๏ƒ

 1"""
 2TIER: beginner (35)
 3GOAL: disposal_method_names takes a LIST - complex resources name every
 4      teardown verb they need, and the printed order documents how the
 5      runtime walks them.
 6SURFACE EXERCISED: bind(disposal_method_names=["flush", "close"])
 7"""
 8import melder as md
 9
10STAGES: list[str] = []
11
12
13class BufferedWriter:
14    """Expose two disposal methods whose requested order is observable."""
15
16    def flush(self) -> None:
17        """Record the flush step before the resource is closed."""
18        STAGES.append("flushed")
19
20    def close(self) -> None:
21        """Record the close step after flushing."""
22        STAGES.append("closed")
23
24
25def main() -> None:
26    """Bind ordered disposal names, create the writer, and verify exact cleanup order."""
27    book = md.Spellbook()
28    book.bind(spell=BufferedWriter, existence="unique",
29              disposal_method_names=["flush", "close"])
30    conduit = book.conjure()
31    writer = conduit.meld("BufferedWriter")
32    assert isinstance(writer, BufferedWriter)
33
34    conduit.cleanup()
35    book.cleanup()
36    print("teardown stages observed:", STAGES or "(documented by the 3.14t run)")
37    assert STAGES == ["flushed", "closed"], "disposal methods must run in supplied order"
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.

More beginner examples ยท Level guide