Let’s use our newly acquired knowledge about context managers to tackle a problem data scientists often find themselves confronted with.
Classes are abstractions over data and functionality. You can think of a class as a blueprint for an object. We call the objects of a certain class “instances”. These instances can have attributes to contain either primitive values (int, string, etc.) or more complex ones (other class instances,lambdas etc.). Class instances can also have so called methods (defined by its class) for modifying its attributes.
Given the ConnectionPool example from the Python Tipp 1: How to write a class based context manager, wa want to proceed to write a generator based context manager. Since we have already discussed the advantages of context manager vs. conventional resource handling we will just include the ConnectionPool example for brevity and prcoeed with the generator based example:
Sometimes you catch yourself writing code that is full of try and except blocks to deal with a component that has an open and close function.