Understanding the Python ‘with’ statement

I don’t know why no one has mentioned this yet, because it’s fundamental to the way with works. As with many language features in Python, with behind the scenes calls special methods, which are already defined for built-in Python objects and can be overridden by user-defined classes. In with‘s particular case (and context managers more generally), the methods are __enter__ and __exit__.

Remember that in Python everything is an object — even literals. This is why you can do things like 'hello'[0]. Thus, it does not matter whether you use the file object directly as returned by open:

with open('filename.txt') as infile:
    for line in infile:
        print(line)

or store it first with a different name (for example to break up a long line):

the_file = open('filename' + some_var + '.txt')
with the_file as infile:
    for line in infile:
        print(line)

Because the end result is that the_file, infile, and the return value of open all point to the same object, and that’s what with is calling the __enter__ and __exit__ methods on. The built-in file object’s __exit__ method is what closes the file.

Leave a Comment