Python 101 : Context managers - "with" expression -


Context managers give you a way to allocate and release resources depending on your needs. 

To define context managers we make use of the the expression "with":



The below two blocks are identical:


Remark:

Defining both "__enter__()" and "__exit__()" functions in the above class allows us to use it with the "with" structure as below: 

Comments

Leave as a comment:

Archive