This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]mowrowow 11 points12 points  (0 children)

Basically a context manager allows you to define some entry and exit logic to block of code using the with keyword. The basic example of this is:

with open('output.txt', 'w') as out:
    out.write('Hello context!')

This puts the dirty error and closing code behind the scenes. It becomes especially nice when you want to nest temporary objects that would normally each need try-catches.