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.
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.
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.