Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python - Context Managers



Context managers in Python provide a powerful way to manage resources efficiently and safely. Acontext manager in Python is an object that defines a runtime context for use with thewith statement. It ensures that setup and cleanup operations are performed automatically.

For instance, when working with file operations, context managers handle the opening and closing of files, ensuring that resources are managed correctly.

How Context Managers Work?

Python context managers work by implementing the__enter__() and__exit__() methods (or their asynchronous equivalents for async operations). These methods ensure that resources are correctly acquired and released. Also, Python'scontextlib module further simplifies the creation of custom context managers.

Example

Here's a simple example demonstrating how a context manager works with file operations in Python.

with open('example.txt', 'w') as file:    file.write('Hello, Tutorialspoint!')

In this example, a file is opened in the write mode, and then automatically closed when the block inside thewith statement is exited.

Python Context Manager Types

Python supports both synchronous and asynchronous context managers. Each type has specific methods that need to be implemented to manage the life cycle of the context.

Synchronous Context Managers

A synchronous context managers are implemented using the__enter__() and__exit__() methods.

1. The __enter__() Method

The__enter__(self) method is called when execution enters the context of the with statement. This method should return the resource to be used within the with block.

Example

Here is a simple example of creating our own context manager using the__enter__() and__exit__() methods.

class MyContextManager:   def __enter__(self):      print("Entering the context")      return self   def __exit__(self, exc_type, exc_value, traceback):      print("Exiting the context")        with MyContextManager():   print("body")

On executing the above code you will get the followingoutput

Entering the contextbodyExiting the context

2. The __exit__() Method

The__exit__(self, exc_type, exc_value, traceback) method is called when execution leaves the context of the with statement. It can handle exceptions if any occur, and it returns a Boolean flag indicating if the exception should be suppressed.

This example demonstrates creating the our own context manager and how the__exit__() methods handle exceptions.

class MyContextManager:   def __enter__(self):      print("Entering the context")      return self   def __exit__(self, exc_type, exc_value, traceback):      print("Exiting the context")      if exc_type:         print("An exception occurred")      return True  # Suppress exceptionwith MyContextManager():   print("body")   name =  "Python"/3 #to raise an exception

While executing the above code you will get the followingoutput

Entering the contextbodyExiting the contextAn exception occurred

Asynchronous Context Managers

Similar to the synchronous context managers, Asynchronous context managers are also implemented using the two methods which are__aenter__() and__aexit__(). These are used withinasync with statements.

The __aenter__(self) Method − It must return an awaitable that will be awaited when entering the context.

__aexit__(self, exc_type, exc_value, traceback) Method − It must return an awaitable that will be awaited when exiting the context.

Example

Following is the example of creating an asynchronous context manager class −

import asyncioclass AsyncContextManager:   async def __aenter__(self):      print("Entering the async context class")      return self   async def __aexit__(self, exc_type, exc_value, traceback):      print("Exiting the async context class")      if exc_type:         print("Exception occurred")      return Trueasync def main():   async with AsyncContextManager():      print("Inside the async context")      name =  "Python"/3 #to raise an exceptionasyncio.run(main())

On executing the above code you will get the following output −

Entering the async context classInside the async contextExiting the async context classException occurred

Creating Custom Context Managers

Thecontextlib module from the Python standard library provides the utilities to create context managers more easily.

Using the contextlib.contextmanager() Function

Thecontextlib.contextmanager() function is a decorator allows you to create factory functions for with statement context managers. It eliminates the need to define a separate class or implement the__enter__() and__exit__() methods individually.

Example

Here's an example using thecontextlib.contextmanager to create a context manager function.

from contextlib import contextmanager@contextmanagerdef my_context_manager():   print("Entering the context manager method")   try:      yield   finally:      print("Exiting the context manager method")with my_context_manager():   print("Inside the context")

On executing the above code you will get the following output −

Entering the context manager methodInside the contextExiting the context manager method

Using the contextlib.asynccontextmanager() Function

Thecontextlib module also provides asynccontextmanager, specifically designed for creating asynchronous context managers. It is similar to contextmanager and eliminates the need to define a separate class or implement the__aenter__() and__aexit__() methods individually.

Example

Here's an example demonstrating the usage ofcontextlib.asynccontextmanager() to create an asynchronous context manager function.

import asynciofrom contextlib import asynccontextmanager@asynccontextmanagerasync def async_context_manager():   try:      print("Entering the async context")      # Perform async setup tasks if needed      yield   finally:      # Perform async cleanup tasks if needed      print("Exiting the async context")async def main():   async with async_context_manager():        print("Inside the async context")      await asyncio.sleep(1)  # Simulating an async operation# Run the asyncio event loopasyncio.run(main())

On executing the above code you will get the following output −

Entering the async contextInside the async contextExiting the async context
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp