With

This lesson introduces the use of with and context management with synchronization primitives.

We'll cover the following...

With

Programs often use resources other than CPU time, including access to local disks, network sockets, and databases etc. The usage pattern is usually a try-except-finally block. Any cleanup actions are performed in the finally block. An alternative to the usual boilterplate code is to use the with statement. The with statement wraps the execution of a block of statements in a context ...