You should be using contextlib in Python
- by Jaakko Moisio
- Nov. 28, 2020
![Captain Picard meme: Your Python class needs to cleanup? Then make it support with statement.](/media/filer_public_thumbnails/filer_public/db/4c/db4ce8a6-7fcf-4b52-88bf-e74b3392d00d/context-managing-picard.jpg__120x120_q85_crop_subject_location-298%2C156_subsampling-2.jpg)
Context managers are the preferred way to write readable and robust Python code that doesn’t leak resources. contextlib makes writing and using context managers a breeze. Check out some examples for productive use.