Created
April 1, 2014 17:45
-
-
Save l04m33/9919234 to your computer and use it in GitHub Desktop.
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 SwitchSign(Exception): | |
| pass | |
| class BreakOut(Exception): | |
| pass | |
| def inner(): | |
| coef = 1 | |
| total = 0 | |
| while True: | |
| try: | |
| input_val = yield total | |
| total = total + coef * input_val | |
| except SwitchSign: | |
| coef = -(coef) | |
| except BreakOut: | |
| return total | |
| def outer1(): | |
| print("Before inner(), I do this.") | |
| i_gen = inner() | |
| input_val = None | |
| ret_val = i_gen.send(input_val) | |
| while True: | |
| try: | |
| input_val = yield ret_val | |
| ret_val = i_gen.send(input_val) | |
| except StopIteration: | |
| break | |
| except Exception as err: | |
| try: | |
| ret_val = i_gen.throw(err) | |
| except StopIteration: | |
| break | |
| print("After inner(), I do that.") | |
| def outer2(): | |
| print("Before inner(), I do this.") | |
| yield from inner() | |
| print("After inner(), I do that.") | |
| if __name__ == '__main__': | |
| import code | |
| shell = code.InteractiveConsole(globals()) | |
| shell.interact() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment