Created
November 8, 2008 08:17
-
-
Save gvx/23036 to your computer and use it in GitHub Desktop.
A switch function for Python
This file contains 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
#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