What are the real performance advantages of using
with open(__file__, 'r') as f:
instead of using:
open(__file__,'r')
in Python 3 for both writing and reading files?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
What the with statement basically does is take advantage of two new magic keywords on an object:
__enter__and__exit__to implement automatic cleanup (c++ destructors, .net IDisposable, etc). So what effectively happens is as follows:Feel free to read a bit more about the actual implementation in pep-0343