Runtime and Context Modules

atexit

Registers callbacks for normal interpreter shutdown, in reverse registration order. Callbacks are not guaranteed on fatal errors, unhandled termination signals or os._exit. unregister removes matching callbacks. This example registers and removes a callback without ending the interpreter.

Formal Syntax & Structure

python syntaxpython
import atexit

Return Value

module

Module namespace available through import; import is a statement, not a value-returning expression.

Working Example

Example Snippetpython
import atexitdef cleanup():    print("cleanup")registered = atexit.register(cleanup)try:    print(registered is cleanup)finally:    atexit.unregister(cleanup)
Python Reference