Python: context manager

Most python programmer unconsciously use context manager without knowing its name. Have you ever opened a file using python? It is a high chance that you use it already. Yes it is the with syntax.

Context manager takes care or resource management such as setup (open) a connection and clean it up when it is no longer used. As mentioned above, one example of it is when we open a file. We use this syntax below.

with open('file_path/file_name.extension', mode) as f:
    for line in f:
        print(line)
        # do something