


A detailed explanation of Python context managers and with blocks
This article mainly introduces the relevant information of Python context manager and with block in detail. It has certain reference value. Interested friends can refer to
Context Manager and with block, the specific content is as follows
The purpose of the context manager object is to manage the with statement, just like the existence of the iterator is to manage the for statement.
The purpose of the with statement is to simplify the try/finally pattern. This mode is used to ensure that a certain operation is performed after a piece of code has finished running. Even if the code is terminated due to an exception, a return statement, or a sys.exit() call, the specified operation will be performed. The code in the finally clause is usually used to release important resources or restore temporarily changed state.
==The context manager protocol contains two methods == enter and exit. When the with statement starts running, the enter method is called on the context manager object. After the with statement finishes running, the exit method is called on the context manager object, thus acting as the finally clause.
==The result of executing the expression after with is the context manager object. Binding the value to the target variable (as clause) is the result of calling the enter method on the context manager object== . The as clause of the with statement is optional. For the open function, an as clause must be added to obtain a reference to the file. However, some context managers will return None because there is no useful object to provide to the user.
with open('mirror.py') as fp: ...
Customized context class:
class A: def __init__(self, name): self.name = name def __enter__(self): print('enter') return self.name def __exit__(self, exc_type, exc_val, exc_tb): print('gone') with A('xiaozhe') as dt: print(dt)
contextlib module
# There are also some classes and other functions in the ##contextlib module, which are used more widely. closing: If the object provides a close() method but does not implement theenter/exit protocol, you can use this function to build a context manager. suppress: Construct a context manager that temporarily ignores the specified exception.
@contextmanager: ==This decorator turns a simple generator function into a context manager==, so there is no need to create a class to implement the manager protocol.
ContextDecorator: This is a base class used to define class-based context managers. This context manager can also be used to decorate functions, running the entire function in a managed context
ExitStack: This context manager can enter multiple context managers. At the end of the with block, ExitStack calls the
exit methods of each context manager in the stack in last-in-first-out order.
Using @contextmanager
@contextmanager decorator can reduce the amount of boilerplate code to create a context manager. Instead of writing a complete class defining enter and exit methods, only You need to implement a generator with a yield statement to generate the value you want the enter method to return. In a generator decorated with @contextmanager, the function of the yield statement is to divide the definition body of the function into two parts: All the code before the ==yield statement is at the beginning of the with block (that is, the interpreter calls the enter method ), the code following the yield statement executes == at the end of the with block (that is, when the exit method is called).import contextlib @contextlib.contextmanager def test(name): print('start') yield name print('end') with test('zhexiao123') as dt: print(dt) print('doing something')
Implementation principle
contextlib.contextmanager The decorator will wrap the function into a class that implements the enter and exit methods. The name of the class is _GeneratorContextManager. The enter method of this class has the following functions:1. Call the generator function and save the generator object (here it is called gen).
2. Call next(gen) and execute to the location of the yield keyword.
3. Return the value produced by next(gen) so that the produced value can be bound to the target variable in the with/as statement.
2. Otherwise, call next(gen) to continue executing the code after the yield statement in the generator function definition body.
Exception handling
In order to tell the interpreter that the exception has been handled, the exit method will return True, and the interpreter will suppress the exception. If the exit method does not explicitly return a value, the interpreter gets None and bubbles up the exception. When using the @contextmanager decorator, the default behavior is the opposite: the exit method provided by the decorator assumes that all exceptions sent to the generator have been handled, so exceptions should be suppressed. If you do not want @contextmanager to suppress exceptions, you must explicitly rethrow the exception in the decorated function. There is a bug in the above code: if an exception is thrown in the with block, the Python interpreter will catch it and then throw it again in the yield expression of the test function. However, there is no code to handle the error, so the test function aborts.使用 @contextmanager 装饰器时,要把 yield 语句放在 try/finally 语句中,因为我们永远不知道上下文管理器的用户会在 with 块中做什么。
import contextlib @contextlib.contextmanager def test(name): print('start') try: yield name except: raise ValueError('error') finally: print('end') with test('zhexiao123') as dt: print(dt) print('doing something')
The above is the detailed content of A detailed explanation of Python context managers and with blocks. For more information, please follow other related articles on the PHP Chinese website!

Python's flexibility is reflected in multi-paradigm support and dynamic type systems, while ease of use comes from a simple syntax and rich standard library. 1. Flexibility: Supports object-oriented, functional and procedural programming, and dynamic type systems improve development efficiency. 2. Ease of use: The grammar is close to natural language, the standard library covers a wide range of functions, and simplifies the development process.

Python is highly favored for its simplicity and power, suitable for all needs from beginners to advanced developers. Its versatility is reflected in: 1) Easy to learn and use, simple syntax; 2) Rich libraries and frameworks, such as NumPy, Pandas, etc.; 3) Cross-platform support, which can be run on a variety of operating systems; 4) Suitable for scripting and automation tasks to improve work efficiency.

Yes, learn Python in two hours a day. 1. Develop a reasonable study plan, 2. Select the right learning resources, 3. Consolidate the knowledge learned through practice. These steps can help you master Python in a short time.

Python is suitable for rapid development and data processing, while C is suitable for high performance and underlying control. 1) Python is easy to use, with concise syntax, and is suitable for data science and web development. 2) C has high performance and accurate control, and is often used in gaming and system programming.

The time required to learn Python varies from person to person, mainly influenced by previous programming experience, learning motivation, learning resources and methods, and learning rhythm. Set realistic learning goals and learn best through practical projects.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1
Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool