Last active
May 11, 2016 08:04
-
-
Save zjyExcelsior/a2df7aa40a55493c7140d0aae8392360 to your computer and use it in GitHub Desktop.
A replacement method for switch statement in 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
# coding: utf-8 | |
def switch(item): | |
return { | |
'one': 1, | |
'two': 2, | |
'three': 3 | |
}.get(item, 0) | |
if __name__ == '__main__': | |
print switch('one') | |
print switch('two') | |
print switch('three') | |
print switch('default') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment