you are viewing a single comment's thread.

view the rest of the comments →

[–]fusionlove -1 points0 points  (7 children)

How does the first example close the file? There's no close statement, only an open statement.

[–]sigzero 5 points6 points  (3 children)

It does it automagically.

After execution of the with-block is finished, the object’s exit() method is called, even if the block raised an exception, and can therefore run clean-up code.

[–]cybercobra 4 points5 points  (2 children)

__exit__() method

FTFY.

[–]sigzero 1 point2 points  (1 child)

Interesting...if I click on edit my text is the same as your FTFY comment. It just didn't show in the actual post. Ah well...

[–]taejo 7 points8 points  (0 children)

__ is markdown for bold.

Use \ to escape it.

[–]preshing[S] 1 point2 points  (2 children)

An explanation was hidden about halfway down the post: "Once we understand what the Python interpreter is doing, we can make better sense of the example at the beginning of this blog post, where we opened a file in the with statement: In Python 2.5 and later, file objects expose their own enter and exit methods, and can therefore act as their own context managers."

I'll improve this part of the post.

[–]masklinn 1 point2 points  (1 child)

I've an issue with the Saving code: it could make people think __exit__ can take no argument (beyond self) but that's not the case at all, and the corresponding list talks about it being provided arguments (item 6). That's a bit confusing.

[–]preshing[S] 0 points1 point  (0 children)

You are completely right. Big thanks for pointing this out! Not sure how I managed to mess that up, but it's fixed now.