Advertisement
rfmonk

contextlib_api_other_project.py

Jan 16th, 2014
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.57 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3.  
  4. class WithinContext(object):
  5.  
  6.     def __init__(self, context):
  7.         print 'WithinContext.__init__(%s)' % context
  8.  
  9.     def do_something(self):
  10.         print 'WithinContext.do_something()'
  11.  
  12.     def __del__(self):
  13.         print 'Withincontext.__del__'
  14.  
  15.  
  16. class Context(object):
  17.  
  18.     def __init__(self):
  19.         print 'Context.__init()'
  20.  
  21.     def __enter__(self):
  22.         print 'Context.__enter__()'
  23.         return WithinContext(self)
  24.  
  25.     def __exit__(self, exc_type, exc_val, exc_tb):
  26.         print 'Context.__exit__()'
  27.  
  28. with Context() as c:
  29.     c.do_something()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement