Last active
October 18, 2020 12:08
-
-
Save revuel/fb7dfafa00d6db09f05bc5835c80ed08 to your computer and use it in GitHub Desktop.
Dummy way to use switch like method in python 3
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
""" 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