Python Exit handlers (atexit) Last Updated : 29 Jun, 2022 Comments Improve Suggest changes Like Article Like Report atexit is a module in python which contains two functions register() and unregister(). The main role of this module is to perform clean up upon interpreter termination. Functions that are registered are automatically executed upon interpreter termination. Whenever a program is killed by a signal not handled by Python, when os.exit() is called, or Python fatal internal error is detected, the functions registered via this module are not executed. register(): Register function takes a function as an argument that is to be executed at interpreter termination. If there are multiple functions passed as arguments e.g. (fun1(), fun2()..) then there execution will be in reverse order (...fun2(), fun1()). The execution occurs in last in first out (LIFO) concept. Syntax: atexit.register(fun, *args, **kwargs) Parameters: First the function name is mentioned and then any arguments for that function is passed. The parameters are separated using ', '. Return: This function returns the called fun and hence the calling can be traced. Note: This function can also be used as a decorator. # Example 1: Python3 # Python program to demonstrate # atexit module import atexit names = ['Geeks', 'for', 'Geeks'] def hello(name): print (name) for name in names: # Using register() atexit.register(hello, name) Output : Geeks for Geeks # Example 2: Using register as a decorator Python3 # Python program to demonstrate # atexit module import atexit # Using register() as a decorator @atexit.register def goodbye(): print("GoodBye.") Output : GoodBye.unregister(): The unregister() function removes the specified fun from the functions defined in the program. It provides a surety that the fun will not be called when the interpreter terminates. Syntax: atexit.unregister(fun) Parameters: The function may or may not contain any parameter. If any present then the fun name is to be specified. Return: No return. Example: Python3 # Python program to demonstrate # atexit module import atexit names = ['Geeks', 'for', 'Geeks'] def hello(name): print (name) for name in names: # Using unregister() atexit.unregister(hello) Output : No Output Comment More infoAdvertise with us Next Article Python Exit handlers (atexit) H HarshitaSahai Follow Improve Article Tags : Python python-modules Practice Tags : python Similar Reads Python | os._exit() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os._exit() method in Python is used to exit the process with specified status wit 2 min read Context Manager in Python In any programming language, the usage of resources like file operations or database connections is very common. But these resources are limited in supply. Therefore, the main problem lies in making sure to release these resources after usage. If they are not released then it will lead to resource l 3 min read __exit__ in Python Context manager is used for managing resources used by the program. After completion of usage, we have to release memory and terminate connections between files. If they are not released then it will lead to resource leakage and may cause the system to either slow down or crash. Even if we do not re 3 min read anext() in Python anext() is a built-in function that retrieves the next item from an asynchronous iterator, acting as the async version of next(). It is essential when working with async iterators and generators, offering more flexibility in asynchronous workflows. Note: anext() is available starting in Python 3.10. 3 min read Python | os.isatty() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.isatty() method in Python is used to check whether the specified file descript 2 min read Built-in Modules in Python Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include Python built-in modules and ext 9 min read Detect script exit in Python Python is a scripting language. This means that a Python code is executed line by line with the help of a Python interpreter. When a python interpreter encounters an end-of-file character, it is unable to retrieve any data from the script. This EOF(end-of-file) character is the same as the EOF that 2 min read aiter() in Python aiter() is a built-in function that returns an asynchronous iterator object from an asynchronous iterable. This allows us to iterate over asynchronous sequences, making it ideal for non-blocking operations in asynchronous programs. It is commonly used with async for loops to iterate over data that i 3 min read Inheritance in Python Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class (called a child or derived class) to inherit attributes and methods from another class (called a parent or base class). This promotes code reuse, modularity, and a hierarchical class structure. In this arti 7 min read Python | os.abort() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.abort() method in Python is used to generate a SIGABRT signal to the current p 2 min read Like