atexit - Exit handlers
It performs cleanup after the interpreter exits.
It has two functions:
- It takes a function as an argument to be executed when the interpreter exits. Execution takes place on a LIFO.
""" Output: Goodbye """ from atexit import register @register def goodbye(): print("Goodbye")
- After calling
<function_name>is guaranteed not to be called when the interpreter shuts down, even if it was registered more than once.
"""Output: No output """ from atexit import unregister names = ['Geeks', 'for', 'Geeks'] def hello(name): print(name) for name in names: atexit.unregister(hello)