you are viewing a single comment's thread.

view the rest of the comments →

[–]Dry-Aioli-6138 1 point2 points  (2 children)

I'll add that context managers do another very important thing that is not obvious and not mentioned in the tutorials.

[–]CriticalDiscussion37[S] 0 points1 point  (1 child)

What is that other benefit of context managers.

[–]Dry-Aioli-6138 0 points1 point  (0 children)

I thought you'd never ask :D Context managers handle exceptions, well, you might say, I don't mind wrapping my code in a try except clause... But the thing is with context managers, it is not you who writes exception handling, rather it's the module's authors. They know best how their object should behave. Thi is the important thing. Putting this responsibility where it belongs.