Skip to content

Instantly share code, notes, and snippets.

@dcolish
Created December 30, 2010 19:07
Show Gist options
  • Select an option

  • Save dcolish/760150 to your computer and use it in GitHub Desktop.

Select an option

Save dcolish/760150 to your computer and use it in GitHub Desktop.
I really wanted to show how much you can abuse a language even if the designers try to stop you.
class FooManager(object):
def __init__(self, data, trys_left=3):
self.data = data
self.trys_left = trys_left
def __enter__(self):
return self
def badfunc(self):
try:
print "%s: %d" % (self.data, self.trys_left)
raise
except:
return
def __exit__(self, type, value, traceback):
if self.trys_left <= 0:
return
with FooManager(self.data, self.trys_left - 1) as F:
F.badfunc()
with FooManager("hello") as foo:
foo.badfunc()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment