Skip to content

Instantly share code, notes, and snippets.

@pasberth
Created December 14, 2011 04:55
Show Gist options
  • Select an option

  • Save pasberth/1475322 to your computer and use it in GitHub Desktop.

Select an option

Save pasberth/1475322 to your computer and use it in GitHub Desktop.
pythonでもcase-when的なものを。
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