Created
December 30, 2010 19:07
-
-
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.
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 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