Created
December 14, 2011 04:55
-
-
Save pasberth/1475322 to your computer and use it in GitHub Desktop.
pythonでもcase-when的なものを。
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
| from contextlib import contextmanager | |
| class CaseContext(object): | |
| def __init__(self, that): | |
| self.that = that | |
| def when(self, *cases): | |
| for a in cases: | |
| if a != self.that: | |
| continue | |
| yield a | |
| break | |
| @contextmanager | |
| def case(that): | |
| yield CaseContext(that) | |
| with case("mana") as that: | |
| for it in that.when("dollias"): | |
| print it | |
| for it in that.when("mana"): | |
| print it |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment