Skip to main content

Python with Context Manager

·170 words·1 min
Posts YT Essential 101 python
Table of Contents
Always prefer with over try-finally.

An essential practices on cleaning up your code using Context Manager in Python.

Using a context manager, with, and let the object handle its own cleanup, is an essential coding practices in Python.

Why Context Manager?
#

There are 4 common use cases where we need to manage the cleanup of codes to free up system resources. The worst case of not doing so will lead to memory leaks, slowing down the entire system or even crash.

  1. Open a file for writing.
  2. Open a socket for connection.
  3. Open a temporary files.
  4. Open a lock.

To make things more complex, an exception could be raised during the 4 use cases above. Using a Context Manager can make Python to ensure all the cleanup will happen automagically.

def main():
    with open('test1.txt') as fp1, open('test2.txt', 'w') as fp2:
        fp2.write(fp1.read())

YouTube Tutorial
#

In this video, mCoding has demonstrated on using with as context manager. He also compare it with the use of try-finally method to ensure code cleanup.

Related

Python Package
·223 words·2 mins
Posts Essential 101 python
Fundamental building block for Python package.
Why __name__ == '__main__'?
·323 words·2 mins
Posts Essential 101 python
Wonder why we always start with if __name__ == '__main__': in Python?
Python Project
·221 words·2 mins
Posts Essential 101 python py-venv
Elegant way to execute Python project.