Skip to content

Instantly share code, notes, and snippets.

@sum-catnip
Created July 6, 2020 09:16
Show Gist options
  • Save sum-catnip/9cca763b3bb0fd7b32f3e909e082f47a to your computer and use it in GitHub Desktop.
Save sum-catnip/9cca763b3bb0fd7b32f3e909e082f47a to your computer and use it in GitHub Desktop.
the legendary python switch case
def switch(target, dict):
for k, v in dict.items():
# could add pattern matching memes here
# instead of just equals
# or maybe the key could be a callable too which will return true or false
# that kinda would just be an if tho
if k == target:
v(target)
break
switch(1, {
15: lambda x: print(15),
'kektop': lambda x: print('kektop'),
1: lambda x: print('yeet'),
"1": lambda x: print('oof')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment