Skip to content

Instantly share code, notes, and snippets.

@revuel
Last active October 18, 2020 12:08
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 revuel/fb7dfafa00d6db09f05bc5835c80ed08 to your computer and use it in GitHub Desktop.
Save revuel/fb7dfafa00d6db09f05bc5835c80ed08 to your computer and use it in GitHub Desktop.
Dummy way to use switch like method in python 3
""" Switch example for python 3 """
def _say_hello() -> str:
return 'Hello'
def _say_goodbye() -> str:
return 'Good bye'
def _unknown_salute() -> str:
return 'Unknown salute :('
# Cases are outside the "switch" method in order to create them just once
_CASES = {0: _say_hello, 1: _say_goodbye}
def switch(case: int) -> str:
"""
Switch like method
:param case: Case (option)
:return: Case evaluation
"""
return _CASES.get(case, _unknown_salute)()
if __name__ == '__main__':
print(f'{switch(0)}, {switch(1)}, {switch(2)}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment