Created
July 13, 2012 18:09
-
-
Save nishio/3106388 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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