Created
December 25, 2017 09:26
-
-
Save veirus/b077f27ae7657e5cc944aaf9bfa4b7e5 to your computer and use it in GitHub Desktop.
dict instead of switch block
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
def a(): | |
print("a") | |
def b(): | |
print("b") | |
def default(): | |
print("default") | |
c = lambda: print('This is "c" case.') | |
def printdic(x): | |
{1: a, 2: b, 'c': c}.get(x, default)() | |
if __name__ == '__main__': | |
import sys | |
printdic(sys.argv[1] if len(sys.argv) > 1 else "c") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hack on hack: not very conventional use of dict + kind of nasty one-line guard for getting argument from command line (it fails in some cases).