How is Python WITH statement implemented?

The python with statement is a little strange to programmers of other languages. It is frequently occurred in tensorflow related codes like:

with tf.Session() as sess:
    do something

A new python programmer is often taught this: with cares about releasing resource after the code in the with block is executed so you do not need to release the resource yourself. Without knowing the internals of the with statement, you’ll possibly get the misunderstanding that the object following the “as” keyword is the expression value following the “with” keyword, and you’re wondering how does with know the way to release the resource.

In fact, there are more things to do for “with” than evaluating the expression after it and assigning it to what follows “as”. After evaluating the value of the expression following the “with” keyword, it also calls the __enter___() method of that value(which means the value of the expression must be an object that implements the __enter__ method). Then it assigns the returned value of __enter__ to the object after “as”. After the code in the with block is executed, it calls the __exit__ method of that value object. The __enter__ and __exit__ methods can do anything you want and return anything, so the object after “as” may not refer to the same object as that after “with”. But __enter__ can return the object itself, in which case the object after “as” is the same as the one after “with”. For example,

with open("my.txt","r") as f
     f.read()

the open function returns a file object, then the __enter__ method of the file object is immediately called, which returns the file object itself. So f is the same as the return value of the open function.

The __exit__ method, of course, is responsible for releasing the resource, such as closing the opened file or tearing down a connection, etc. reference.

If you like my content, please consider buying me a coffee. Buy me a coffeeBuy me a coffee Thank you for your support!

Leave a Reply