Skip to main content
πŸ”’ Preview mode. The first 15 Foundations lessons are free; this one is Pro. Start a 7-day trial to unlock the editor, AI hints and the rest of the curriculum. Card required, cancel any time in Dashboard.Start 7-day trial β†’
⚑
← Coursesβ€ΊSenior Deep-DivesModule 1 Β· Concurrency internals Β· Recapβ€ΊExitStack for dynamic context managementpredict23 / 161
+100 XP
Task
πŸ“ **Question:** Predict the exact output. \`res(name)\` is a tiny context manager that prints \`open:\` / \`close:\`. Three resources enter an \`ExitStack\` in order A β†’ B β†’ C. When the \`with\` exits, what order do they close in? πŸ“‹ Pick the right answer. πŸ’‘ **Hint:** Re-read the theory above if unsure.
Predict the output

Read the code carefully

from contextlib import ExitStack, contextmanager


@contextmanager
def res(name):
    print(f"open:{name}")
    try:
        yield name
    finally:
        print(f"close:{name}")


with ExitStack() as stack:
    a = stack.enter_context(res("A"))
    b = stack.enter_context(res("B"))
    c = stack.enter_context(res("C"))
    print(f"use:{a}{b}{c}")

print("done")

What will the program print? Write here: