Skip to content

Instantly share code, notes, and snippets.

@munhitsu
Created February 28, 2013 11:50
Show Gist options
  • Save munhitsu/5056212 to your computer and use it in GitHub Desktop.
Save munhitsu/5056212 to your computer and use it in GitHub Desktop.
demo of context manager and loop break/continue behaviour
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