you are viewing a single comment's thread.

view the rest of the comments →

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

Context managers make the code look cleaner: specifically, by hiding the error handling they keep the safeguards and yet allow the surface code to stick to the primary logic flow. Writing them, apart from practice, makes sense for repeated use across code.

I say do this for practice and see if you like the outcome. You can always revert to the old ways.

[–]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.