The Startup
Published in

The Startup

Python Coding Tip: Using the ‘With’ Statement Instead ‘Try…Finally’

The ‘with’ statement and Context Managers in Python, two ways to implement the context management protocol

As a programmer, we usually use try/expect/finally patterns to handle resource management operations, such as open a file, connect a database, and acquire a lock.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jason Zhang

Jason Zhang

Software Engineer, Kaggle Competitions Expert