Understand Context Managers in Python and Learn to Use Them in Unit Tests | by Lynn Kwong | Jul, 2022
Use context managers to make your code more robust and more PythonicImage by BUMIPUTRA in PixabayWe have been using the with keyword with the open function to create a context that will close the file after we have finished reading/writing. However, context managers in Python are not limited to managing external resources like file descriptors, database connections, threading locks, etc, they can be used to provide all kinds of contexts which require some setup and teardown actions before and after the main code block is…