Explaining Python’s ‘__enter__’ and ‘__exit__’
Using these magic methods (__enter__, __exit__) allows you to implement objects which can be used easily with the with statement. The idea is that it makes it easy to build code which needs some ‘cleandown’ code executed (think of it as a try-finally block). Some more explanation here. A useful example could be a database connection object (which then automagically closes … Read more