Learn more about the keyword "with" in Python and the context manager

Source: Internet
Author: User
Tags finally block
This article mainly describes the Python keyword "with" and the context manager of the relevant information, the article describes the very detailed, I believe that you learn or use Python has a certain reference value, the need for friends to see together below.

Objective

If you have the habit of reading the source code, you may see some good codes often appear with a "with" keyword statement, which is usually used in what scenario? Say today with and context manager.

For system resources such as files, database connections, sockets, and after the application opens these resources and executes the business logic, one thing that must be done is to close (disconnect) the resource.

For example, the Python program opens a file, writes content to the file, and then closes the file, otherwise what happens? In extreme cases, the "Too Many open files" error occurs because the maximum number of files that the system allows you to open is limited.

Similarly, for a database, if the number of connections is too high to shut down in a timely manner, there may be "can not connect to MySQL server Too many connections" because the database connection is a very expensive resource that cannot be created indefinitely.

Take a look at how to close a file correctly.

Normal version:

Def m1 (): F = open ("Output.txt", "W") F.write ("Zen of Python") F.close ()

There is a potential problem with this writing, and if an exception occurs during the call to write, causing the subsequent code to fail to continue, the Close method cannot be called properly, so the resource is freed by the program occupier. So how do you improve the code?

Advanced version:

def m2 (): F = open ("Output.txt", "W") Try:f.write ("Zen of Python") except Ioerror:print ("Oops Error") Finally:f.close ()

The modified version of the program is a try capture of the code where the exception might occur, using the Try/finally statement, which indicates that if the program has an exception in the try code block, subsequent code is no longer executed and jumps directly to the except code block. In any case, finally block code will eventually be executed. Therefore, as soon as you put close in the finally code, the file must be closed.

Premium Version:

def m3 (): With open ("Output.txt", "W") as F:f.write ("Zen of Python")

A more concise and elegant way is to use the WITH keyword. The return value of the Open method is assigned to the variable F, and the system automatically calls the method when it leaves the with code block, with f.close() the same function as the try/finally statement. So what is the principle of its implementation?

There is another concept to be addressed before the principle of with is the context manager.

Context Manager

Any object that implements __enter__() and __exit__() methods can be called a context manager, and the context Manager object can use the WITH keyword. Obviously, the file object also implements the context manager.

So how do the file objects implement both methods? We can simulate the implementation of a class of its own file, let the class implement __enter__() and __exit__() method.

Class File (): Def __init__ (self, filename, mode): Self.filename = filename Self.mode = mode def __enter__ (self): print ("en Tering ") self.f = open (Self.filename, Self.mode) return self.f def __exit__ (self, *args): Print (" would exit ") Self.f.close ( )

__enter__()method returns the resource object, which is the file object you are about to open, and the __exit__() method handles some cleanup work.

Because the File class implements the context manager, it is now possible to use the WITH statement.

With File (' OUT.txt ', ' W ') as F:print ("writing") F.write (' Hello, Python ')

This way, you don't have to call the Close method, which is called by the system automatically, even if an exception is encountered in the middle of the close method.

Contextlib

Python also provides a contextmanager adorner that further simplifies the way the context manager is implemented. The function is split into two parts through yield, the statement before yield is executed in the __enter__ method, and the statement after yield is executed in the __exit__ method. The value immediately following the yield is the return value of the function.

From contextlib import contextmanager@contextmanagerdef my_open (path, mode): F = open (path, mode) yield F f.close ()

Call

With My_open (' OUT.txt ', ' W ') as F:f.write ("Hello, the simplest context manager")

Summarize

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.