Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

With...as... in Python How to use grammar

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to use with...as... grammar in Python". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to use with...as... grammar in Python".

Brief introduction:

With, a new syntax introduced from Python2.5, is a context management protocol that aims to remove all try,except and finally keywords and resource release related code from the flowchart, simplifying try. Please... .finlally 's processing flow

With is initialized by the _ _ enter__ method, and then does the aftermath and handles the exception in _ _ exit__. So objects processed with with must have _ _ enter__ () and _ _ exit__ () methods.

The with statement is suitable for accessing resources to ensure that necessary "cleanup" operations are performed regardless of whether an exception occurs during use, such as automatic closing of files after use, automatic acquisition and release of locks in threads, and so on.

Examples are as follows:

# Open the 1.txt file and print out the contents of the file with open ('1.txtshipping,' ringing, encoding= "utf-8") as f: print (f.read ())

Does this code look like deja vu? Yes, that's right!

1. The basic syntax format of With...as statement: with expression [as target]: with_body

Parameter description:

Expression: an expression that needs to be executed

Target: is a variable or tuple that stores the results returned by the execution of the expression expression. [] indicates that the parameter is optional.

II. The execution process of With...as grammar

First run the expression expression, and if the expression contains evaluation, class initialization, and so on, it will be executed first.

Run the code in the _ _ enter () _ _ method

Run the code in with_body

Run the code in the _ _ exit () _ _ method to clean up, such as releasing resources, handling errors, and so on.

3. Verify the example #! / usr/bin/python3#-*-coding: utf-8-*-"" with...as... Grammar test "" _ _ author__ = "River.Yang" _ _ date__ = "2021-9-5" _ version__ = "1.1.0" class testclass (object): def test (self): print ("test123") print ("") class testwith (object): def _ init__ (self): print ("create testwith class") print ("") Def _ _ enter__ (self): print ("enter with...as.. Former ") print (" create testclass entity ") print (") tt = testclass () return tt def _ _ exit__ (self, exc_type, exc_val, exc_tb): print (" exit with...as... ") Print ("release testclass resources") print (") if _ _ name__ = ='_ main__': with testwith () as t: print (" with...as... program content ") print (" with_body ") t.test () 4. Program running result

Create a testwith class

Enter with...as.. Front

Create a testclass entity

With...as... Program content

With_body

Test123

Exit with...as...

Release testclass resources

5. Code parsing

This code creates a total of two classes. The first testclass class is the function class, which is used to define all the functions we need, such as the test () method here.

The testwith class is what we use to test with...as.... Syntax class that is used to clean up the testclass class (release resources, etc.).

Program execution process:

The above is all the contents of the article "how to use with...as... Grammar in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report