Context Manager

Author

Jakob Johannesson

Published

September 20, 2024

class PrintingContextManager:
    def __enter__(self):
        print('entering the context manager')
        return "i am the returned value"

    def __exit__(self, exc_type, exc_value, traceback):
        print('exiting the context manager')


with PrintingContextManager() as var:
    print('inside the context manager')
    print(var)
entering the context manager
inside the context manager
i am the returned value
exiting the context manager