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 release resources, context managers implicitly performs this task.
Refer the below article to get the idea about basics of Context Manager.
This is a method of
ContextManager class. The
__exit__ method takes care of releasing the resources occupied with the current code snippet. This method must be executed no matter what after we are done with the resources. This method contains instructions for properly closing the resource handler so that the resource is freed for further use by other programs in the OS.
If an exception is raised; its type, value, and traceback are passed as arguments to
__exit__(). Otherwise, three
None arguments are supplied. If the exception is suppressed, then the return value from the
__exit__() method will be
syntax: __exit__(self, exception_type, exception_value, exception_traceback)
exception_type: indicates class of exception.
exception_value: indicates type of exception . like divide_by_zero error, floating_point_error, which are types of arithmetic exception.
exception_traceback: traceback is a report which has all of the information needed to solve the exception.
# Example 1:.
init method called enter method called with statement block exit method called
# Example 2: Understanding parameters of
__exit__(). We will create a context manager that will be used to divide two numbers. If the
Inside __enter__ 3.0 Inside __exit__ Execution type: None Execution value: None Traceback: None ................................................ Inside __enter__ Inside __exit__ Execution type: Execution value: division by zero Traceback: Traceback (most recent call last): File "gfg.py", line 32, in r.divide_by_zero() File "gfg.py", line 21, in divide_by_zero print(self.num1 / self.num2) ZeroDivisionError: division by zero
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.