Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good practice to depend on python's with...as statement

I'm curious if it is considered safe or good practice to depend on python's with...as statement. For example when opening a file:

with open("myfile","w") as myFile:
    #do something

So in this example I neglected to explicitly call myFile.close() however I can assume it was called when python exited the with...as statement by calling the objects __exit__() method. Is it good practice/safe to depend upon this or would it be better to always explicitly call file.close()

like image 710
Blaine Avatar asked Feb 08 '13 16:02

Blaine


People also ask

Why use with AS in Python?

The Takeaway. In Python, the with statement replaces a try-catch block with a concise shorthand. More importantly, it ensures closing resources right after processing them. A common example of using the with statement is reading or writing to a file.

What is a with statement in Python?

The with statement in Python is used for resource management and exception handling. You'd most likely find it when working with file streams. For example, the statement ensures that the file stream process doesn't block other processes if an exception is raised, but terminates properly.

Which methods are involved on entering into and exiting from the block of code written in with statement?

The block of code which uses the acquired resource is placed inside the block of the with statement. As soon as the code inside the with block is executed, the __exit__() method is called. All the acquired resources are released in the __exit__() method. This is how we use the with statement with user defined objects.

What is context management in Python?

A context manager usually takes care of setting up some resource, e.g. opening a connection, and automatically handles the clean up when we are done with it. Probably, the most common use case is opening a file. with open('/path/to/file.txt', 'r') as f: for line in f: print(line)


2 Answers

This is what context managers are for, to rely on them to close the file for you. Context managers are called even if there was an exception.

The alternative is to use an finally block instead:

myFile = open("myfile","w")
try:
    # do something with myFile
finally:
    myFile.close()

but because the block inside of the try: might be long, by the time you get to the finally statement you have forgotten what you were setting this up for.

Context managers are more powerful still. Because the __exit__ method is informed of any exceptions, they can act as exception handlers as well (ignore the exception, raise another, etc.).

like image 138
Martijn Pieters Avatar answered Oct 21 '22 21:10

Martijn Pieters


Yes, the with statement is better way. Since Python 2.5, the file object has been equipped with __enter__() and __exit__() methods. The __exit__() method closes the file object.

Python guarantees that it will call the __exit__() method, but there is not guarantee that the __exit__() method will close the resource, especially with 3rd party code. You need to manually verify that.

like image 26
BigG Avatar answered Oct 21 '22 23:10

BigG