r/pythontips • • Apr 06 '19

Some Python anti-patterns

46 Upvotes

18 comments sorted by

View all comments

3

u/groovitude Apr 06 '19

Good article.

I'll add that the "with" statement is not just for files; it's for anything with setup and teardown logic. You can build your own context managers with enter and exit methods.

3

u/_importantigravity_ Apr 06 '19

Thank you for your feedback!

Yes, that is a valid addendum to this -- using `with` wherever there's a context manager that can be used. The reason only the file's scenario is mentioned here is that it is very common, and in other cases if there's a context manager to be used there's generally no other way -- so people end up using `with` anyway.