PyDocs

Nav

Python Runtime Services

ExitStack.callback

  • ExitStack.callback(callback, /, *args, **kwds)
Registers an arbitrary callback and arguments.

Cannot suppress exceptions.
1 Example
>>> from contextlib import ExitStack
>>> stack = ExitStack()
>>> with stack:
...     stack.callback(print, "Callback: from first context")
...     print("Leaving first context")
...
Leaving first context
Callback: from first context
>>> with stack:
...     stack.callback(print, "Callback: from second context")
...     print("Leaving second context")
...
Leaving second context
Callback: from second context
>>> with stack:
...     stack.callback(print, "Callback: from outer context")
...     with stack:
...         stack.callback(print, "Callback: from inner context")
...         print("Leaving inner context")
...     print("Leaving outer context")
...
Leaving inner context
Callback: from inner context
Callback: from outer context
Leaving outer context
from the Python docs
See Also
No see-alsos for contextlib/ExitStack.callback
0 Notes
No notes for ExitStack.callback