15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

● tHReading.BoundedSemaphore<br />

Since files are first on the list and the simplest example, here is a code snippet of what it looks like to<br />

use a with statement:<br />

with open('/etc/passwd', 'r') as f:<br />

for eachLine in f:<br />

# ...do stuff with eachLine or f...<br />

What this code snippet will do is... well, this is <strong>Python</strong>, so you can probably already guess. It will do<br />

some preliminary work, such as attempt to open the file, and if all goes well, assign the file object to f.<br />

Then it iterates over each line in the file and does whatever processing you need to do. Once the file has<br />

been exhausted, it is closed. If an exception occurs either at the beginning, middle, or end of the block,<br />

then some cleanup code must be done, but the file will still be closed automatically.<br />

Now, because a lot of the details have been pushed down and away from you, there are really two levels<br />

of processing that need to occur: First, the stuff at the user levelas in, the things you need to take care<br />

of as the user of the objectand second, at the object level. Since this object supports the context<br />

management protocol, it has to do some "context management."<br />

10.4.2. *Context Management Protocol<br />

Unless you will be designing objects for users of the with statement, i.e., programmers who will be using<br />

your objects to design their applications with, most <strong>Python</strong> programmers are going to be just users of<br />

the with statement and can skip this optional section.<br />

We are not going into a full and deep discussion about context management here, but we will explain<br />

the types of objects and the functionality that are necessary to be protocol-compliant and thus be<br />

eligible to be used with the with statement.<br />

Previously, we described a little of how the protocol works in our example with the file object. Let us<br />

elaborate some more here.<br />

Context Expression (context_expr), Context Manager<br />

When the with statement is executed, the context expression is evaluated to obtain what is called a<br />

context manager. The job of the context manager is to provide a context object. It does this by invoking<br />

its required __context__() special method. The return value of this method is the context object that will<br />

be used for this particular execution of the with_suite. One side note is that a context object itself can<br />

be its own manager, so context_expr can really be either a real context manager or a context object<br />

serving as its own manager. In the latter case, the context object also has a __context__() method,<br />

which returns self, as expected.<br />

Context Object, with_suite<br />

Once we have a context object, its __enter__() special method is invoked. This does all the preliminary<br />

stuff before the with_suite executes. You will notice in the syntax above that there is an optional as var<br />

piece following context_expr on the with statement line. If var is provided, it is assigned the return value<br />

of __enter__(). If not, the return value is thrown away. So for our file object example, its context<br />

object's __enter__() returns the file object so it can be assigned to f.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!