Skip to content

Instantly share code, notes, and snippets.

@joelgrus
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelgrus/1b726fc49f52946ae710 to your computer and use it in GitHub Desktop.
Save joelgrus/1b726fc49f52946ae710 to your computer and use it in GitHub Desktop.
stealing an idea from trenthauck
# got the idea from https://gist.github.com/tshauck/6ebb0c2d6adc667608b8
def switch(*args, default=None):
try:
return next(value for cond, value in args if cond)
except StopIteration:
return default
x = 1
y = switch((x > 10, "more than 10"),
(x > 1, "more than 1"),
(x > 0, "more than 0"),
default="hooray!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment