spock
6/11/2017 - 11:54 AM

Writing your own context manager

Writing your own context manager

from contextlib import contextmanager

@contextmanager
def managed_file(name):
    try:
        f = open(name, 'w')
        yield f
    finally:
        f.close()