atexit - Exit handlers
-
It performs cleanup after the interpreter exits.
-
It has two functions:
1. register()
- 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")
2. unregister()
- After calling
unregister()
,<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)