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.
- Open a file for writing.
- Open a socket for connection.
- Open a temporary files.
- 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.