ID EN
Contextlib

pop_all

Python 3.11

Transfers the callback stack to a fresh ExitStack instance and returns it. No callbacks are invoked by this operation - instead, they will now be invoked when the new stack is closed (either explicitly or implicitly at the end of a with statement).

Syntax

PYTHON
pop_all()

Examples

Example 1
PYTHON
with ExitStack() as stack:
    files = [stack.enter_context(open(fname)) for fname in filenames]
     Hold onto the close method, but don't call it yet.
    close_files = stack.pop_all().close
     If opening any file fails, all previously opened files will be
     closed automatically. If all files are opened successfully,
     they will remain open even after the with statement ends.
     close_files() can then be invoked explicitly to close them all.

See Also

ExitStack with