class ContextTest(object):

    def __enter__(self):
        print('Enter')

def __exit__(self, type, value, traceback):
    print('Exit')

def test():
    with ContextTest() as foo:
        print('Inside')
return
Open file and with statement in Python