Skip to content

Instantly share code, notes, and snippets.

@nishio
Created July 13, 2012 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nishio/3106388 to your computer and use it in GitHub Desktop.
Save nishio/3106388 to your computer and use it in GitHub Desktop.
"""
init False
enter False
init True
exit False <type 'exceptions.NameError'>
Traceback (most recent call last):
File "with_statement2.py", line 14, in <module>
with Foo(False) as x, Foo(True) as y:
File "with_statement2.py", line 6, in __init__
raise RuntimeException
NameError: global name 'RuntimeException' is not defined
"""
class Foo(object):
def __init__(self, to_fail):
print "init", to_fail
self.to_fail = to_fail
if to_fail:
raise RuntimeException
def __enter__(self):
print "enter", self.to_fail
def __exit__(self, *args):
print "exit", self.to_fail, args[0]
with Foo(False) as x, Foo(True) as y:
print "Hello"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment