Skip to content

Instantly share code, notes, and snippets.

@gvx
Created November 8, 2008 08:17
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 gvx/23036 to your computer and use it in GitHub Desktop.
Save gvx/23036 to your computer and use it in GitHub Desktop.
A switch function for Python
#By Robin Wellner (gvx)
#I hereby waive copyright and related or neighboring rights to this work
#See the Creative Commons Zero Waiver at <http://creativecommons.org/publicdomain/zero/1.0/>
def switch(expression, cases, default=None):
try:
ret = cases[expression]
except KeyError:
ret = default
if callable(ret): ret = ret()
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment