Go to: Understand Python's With statement and pythonwith statement.
Python's with statement provides a very convenient way of dealing with the situation where you have to do a setup and teardown to make something happen. A very good example for this is the situation where you want to gain a handler to a file, read data from the file and the close the file handler. some tasks may need to be configured in advance and cleaned up afterwards. In this scenario, the with statement of Python provides a very convenient processing method. A good example is File Processing. You need to obtain a file handle, read data from the file, and then close the file handle. Without the with statement, one wocould write something along the lines of: if you do not need the with statement, the Code is as follows:
123 |
file = open ( "/tmp/foo.txt" ) data = file .read() file .close() |
There are two annoying things here. first, you end up forgetting to close the file handler. the second is how to handle exceptions that may occur once the file handler has been obtained. one cocould write something like this to get around this: there are two problems here. First, you may forget to close the file handle. Second, an exception occurs when reading data from the file, and no processing is performed. The following is an enhanced version for exception handling:
12345 |
file = open ( "/tmp/foo.txt" ) try : data = file .read() finally : file .close() |
While this works well, it is unnecessarily verbose. this is where with is useful. the good thing about with apart from the better syntax is that it is very good handling exceptions. the above code wowould look like this, when using with: although this code runs well, it is too long. At this time, it is time to show your skills. In addition to more elegant syntaxes, with can also handle exceptions in the context. The code for the with version is as follows:
12 |
with open ( "/tmp/foo.txt" ) as file : data = file .read() |
How does with work?
While this might look like magic, the way Python handles with is more clever than magic. the basic idea is that the statement after with has to evaluate an object that responds to an _ enter _ () as well as an _ exit _ () function. this seems to be full of magic, but it is not just magic. Python is still very clever in handling. The basic idea is that the object with the value must have a _ enter _ () method and a _ exit _ () method. After the statement that follows with is evaluated, the _ enter _ () function on the resulting object is called. the value returned by this function is assigned to the variable following. after every statement in the block is evaluated, the _ exit _ () function is called. after the statement followed by with is evaluated, the _ enter _ () method of the returned object is called, and the return value of this method is assigned to the variable after. After all the code blocks after with are executed, the _ exit _ () method of the returned object is called. This can be demonstrated with the following example: the following example describes how to work:
12345678910111213141516171819 |
#!/usr/bin/env python # with_example01.py class Sample: def __enter__( self ): print "In __enter__()" return "Foo" def __exit__( self , type , value, trace): print "In __exit__()" def get_sample(): return Sample() with get_sample() as sample: print "sample:" , sample |
When executed, this will result in: run the code and output the following:
1234 |
bash - 3.2 $ . / with_example01.py In __enter__() sample: Foo In __exit__() |
As you can see, The _ enter _ () function is executed The value returned by it-in this case "Foo" is assigned to sample The body of the block is executed, thereby printing the value of sample ie. "Foo" The _ exit _ () function is called. what makes with really powerful is the fact that it can handle exceptions. you woshould have noticed that the _ exit _ () function for Sample takes three arguments-v Al, type and trace. these are useful in exception handling. let's see how this works by modifying the above example. as you can see, 1. the _ enter _ () method is executed. 2. the value returned by the _ enter _ () method. In this example, it is "Foo" and is assigned to the variable 'sample' 3. run the code block and print the value of the variable "sample" to "Foo" 4. when the _ exit _ () method is called with, it can handle exceptions. You may have noticed that the _ exit _ method of the Sample class has three parameters-val, type, and trace. These parameters are quite useful in exception handling. Let's change the code to see how it works.
12345678910111213141516171819 |
#!/usr/bin/env python # with_example02.py class Sample: def __enter__( self ): return self def __exit__( self , type , value, trace): print "type:" , type print "value:" , value print "trace:" , trace def do_something( self ): bar = 1 / 0 return bar + 10 with Sample() as sample: sample.do_something() |
Notice how in this example, instead of get_sample (), with takes Sample (). it does not matter, as long as the statement that follows with evaluates to an object that has an _ enter _ () and _ exit _ () functions. in this case, Sample ()'s _ enter _ () returns the newly created instance of Sample and that is what gets passed to sample. in this example, the get_sample () following with is changed to Sample (). This does not matter, as long as the object returned by the statement following with has the _ enter _ () and _ exit _ () methods. In this example, the _ enter _ () method of Sample () returns the newly created Sample object and assigns it to the variable sample. When executed: After the code is executed:
12345678910 |
bash - 3.2 $ . / with_example02.py type : < type 'exceptions.ZeroDivisionError' > value: integer division or modulo by zero trace: <traceback object at 0x1004a8128 > Traceback (most recent call last): File "./with_example02.py" , line 19 , in <module> sample.do_something() File "./with_example02.py" , line 15 , in do_something bar = 1 / 0 ZeroDivisionError: integer division or modulo by zero |
Essential, if there are exceptions being thrown from anywhere inside the block, the _ exit _ () function for the object is called. as you can see, the type, value and the stack trace associated with the exception thrown is passed to this function. in this case, you can see that there was a ZeroDivisionError exception being thrown. people implementing libraries can write code that clean up resource S, close files etc. in their _ exit _ () functions. in fact, when the code block after with throws any exception, the __exit _ () method is executed. As shown in the example, when an exception is thrown, the associated type, value, and stack trace are passed to the _ exit _ () method. Therefore, the error ZeroDivisionError thrown is printed out. During database development, operations such as clearing resources and closing files can all be placed in the _ exit _ method. Thus, Python's with is a nifty construct that makes code a little less verbose and makes cleaning up during exceptions a bit easier. therefore, the with statement of Python provides an effective mechanism to make the code more concise, while cleaning is easier when exceptions are generated. I have put the code examples given here on Github. the sample code can be found on Github. For the original article, see Understanding Python's "With" Statement