What is the with..as.. you often write in Python?

What is the with..as.. you often write in Python?

Xiaoshuai b The correct posture for learning python

The with statement can be said to be one of the classic representatives of "Life is short, I use Python" in Python. It can make us very short and effective when we write code.

To give a simple example, in Java, you want to read the contents of a file. Generally speaking, you need to define a reading method like this:

What is the with..as.. you often write in Python?

In Python, if you want to read a file, you can do this directly:

What is the with..as.. you often write in Python?

It looks pretty short in this way.

What is the with..as.. you often write in Python?

In fact, the try...except..finally involved in file operations are all encapsulated:

What is the with..as.. you often write in Python?

And we can directly manipulate the object through with. Isn't it a bit powerful, then how does it do it?

The next step is: learn the correct posture of python

What is the with..as.. you often write in Python?

The with statement is used to manage the context object, through which you can manipulate the context object:

What is the with..as.. you often write in Python?

For example, in the above, in the Handsomeb object obtained after the with call, the enter () and exit () methods need to be used . In other words, if you want the object you create to be used by with, then you The object must have two methods: enter () and exit ().

What are these two methods used for?

When we use with to execute this object, we will call enter () first, and finally call exit ():

What is the with..as.. you often write in Python?

It prints like this:

What is the with..as.. you often write in Python?

As you can see, we use with to call get_Handsomeb, the obtained object will first execute the "enter method", and then execute the "get..." in with, and then execute the "enter exit method" after all executions are completed ".

Let's take a look at the "as" that follows with.. What is it for?

What is the with..as.. you often write in Python?

The variable name after as here is actually the value returned by the enter method.

What is the with..as.. you often write in Python?

You see, I defined the return "handsomeb" in the enter method, and then used the "with .. as h" method to get the object. From the printed result, you can see: At this time, what h gets is the return value "handsomeb" in the enter method. ".

Can return an object:

What is the with..as.. you often write in Python?

Next, let's take a look at the exit method we defined. If you are careful, you will find that there are several parameters: type, value, and trace.

What do they do? This is the problem we mentioned at the beginning.

When we are operating some objects, such as when operating files, we may find such unknown abnormalities, such as the location of the file cannot be found, the file cannot be opened, and so on. For information about these abnormalities, we can check the exit Method.

for example:

We print out the type, value, and trace parameters, and then create a calculation method:

What is the with..as.. you often write in Python?

However, this method executes 100/0. We know that 0 cannot be used as a divisor, so an error will be reported. Let's use with to call it and see:

What is the with..as.. you often write in Python?

The execution result is this:

What is the with..as.. you often write in Python?

As you can see, these thrown exception information can be directly obtained in the parameters of the exit method, then we can do some things here (handling some exceptions), for example, in file operations, we can go to Finally, you need to close this file stream, but with the exit method, Python directly closes the resource in it, so we don't need to close it manually.

Ok, the above is the working method of with that I shared with you today. To summarize, it is:

1. You can get a context manager through the with statement
2. Execute the object
3. Load the enter method
4. Load the exit method
5. Execute enter
6. As you can get the return value of enter
7. Get the object to perform related operations
8. The execution is complete Then call the exit method
9. If you encounter an exception, exit can get the exception information.
10. Handle the exception in exit and return True.
11. Continue to execute the statement following with.

Hope to help you, see you next time, peace!

Guess you like

Origin blog.51cto.com/15082392/2644477