Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator
atexit.unregister

What is the atexit.unregister method in Python?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The atexit module

atextit is a module in Python that performs clean-up operations upon the interpreter’s termination. This module provides functions similar to shutdown hooks in Java. The registered functions (also called handlers) are executed automatically upon the termination of the interpreter. The functions registered via this module are not executed when a program is terminated by a signal that Python does not handle. The os.exit() command is called, or the Python fatal internal error is detected. The handlers/functions are executed in reverse order with reference to the order in which they were registered.

The unregister() method

The unregister() method removes the given function from the list of functions to be executed at termination. Every occurrence of the given function is removed from the list of the registered handlers if it was registered multiple times.

Syntax

atexit.unregister(func)
  • func: This is the function to be removed.

Example

import atexit
def func_1(args):
print("Executing func_1 with argument - %s" % (args,))
def func_2():
print("Executing func_2 with no arguments")
atexit.register(func_1, [1,2,3])
atexit.register(func_2)
print("Hello Educative")
atexit.unregister(func_2)

Explanation

  • Line 1: We import the atexit module.
  • Lines 3-4: The func_1 function is defined. This takes args as an argument and prints it.
  • Lines 6-7: The func_2 function is defined with no arguments and prints a string.
  • Line 9: The func_1 function is registered as an exit handler using the register method where we pass the argument as a positional argument.
  • Line 10: The func_2 function is registered as an exit handler using the register method. As the function accepts no arguments, no parameters are passed as arguments in the register method.
  • Line 12: The print statement is executed.
  • Line 14: The func_2 function is unregistered from the list of exit handlers using the unregister method.

When we run the code, only func_1 is executed since func_2 is removed.

RELATED TAGS

python
communitycreator
atexit.unregister

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring