#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """Implementing the context manager API by hand. """ #end_pymotw_header class WithinContext(object): def __init__(self, context): print 'WithinContext.__init__(%s)' % context def do_something(self): print 'WithinContext.do_something()' def __del__(self): print 'WithinContext.__del__' class Context(object): def __init__(self): print 'Context.__init__()' def __enter__(self): print 'Context.__enter__()' return WithinContext(self) def __exit__(self, exc_type, exc_val, exc_tb): print 'Context.__exit__()' with Context() as c: c.do_something()