Advertisement
rfmonk

contextlib_closing.py

Jan 16th, 2014
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.49 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3.  
  4. import contextlib
  5.  
  6.  
  7. class Door(object):
  8.     def __init__(self):
  9.         print ' __init__()'
  10.  
  11.     def close(self):
  12.         print ' close()'
  13.  
  14. print 'Normal Example:'
  15. with contextlib.closing(Door()) as door:
  16.     print '  inside with statement'
  17.  
  18. print '\nError handling example:'
  19. try:
  20.     with contextlib.closing(Door()) as door:
  21.         print '  raising from inside with statement'
  22.         raise RuntimeError('error message')
  23. except Exception, err:
  24.     print ' Had an error:', err
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement