Skip to content

Instantly share code, notes, and snippets.

@veirus
Created December 25, 2017 09:26
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 veirus/b077f27ae7657e5cc944aaf9bfa4b7e5 to your computer and use it in GitHub Desktop.
Save veirus/b077f27ae7657e5cc944aaf9bfa4b7e5 to your computer and use it in GitHub Desktop.
dict instead of switch block
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")
@veirus
Copy link
Author

veirus commented Dec 25, 2017

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment