Created
February 28, 2013 11:50
-
-
Save munhitsu/5056212 to your computer and use it in GitHub Desktop.
demo of context manager and loop break/continue behaviour
This file contains hidden or 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
class DebugContext(object): | |
def __init__(self): | |
print self.__class__, "init" | |
def __enter__(self): | |
print self.__class__, "enter" | |
def __exit__(self, type, value, traceback): | |
print self.__class__, type, value, traceback | |
print self.__class__, "exit" | |
i = 0 | |
while True: | |
print "loop start" | |
with DebugContext() as c: | |
i += 1 | |
print "I'm in" | |
if i == 3: | |
print "Breaking" | |
break | |
elif i == 2: | |
print "Continuing" | |
continue | |
print "loop end" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment