I am trying to build a switch logic in python.
Based on Replacements for switch statement in Python? I build the following code:
def func(string):
print(string)
class Switch:
def __init__(self):
pass
def first_switch(self, case):
return {
'1': func('I am case 1'),
'2': func('I am case 2'),
'3': func('I am case 3'),
}.get(case, func('default'))
switch = Switch()
switch.first_switch('2')
This will give me the output:
I am case 1
I am case 2
I am case 3
default
I expected the output to be
I am case 2
Is this dictionary-case-logic only applicable outside of a class definition or did I miss some additional code? Looks like all dictionary key-value pairs are evaluated at the function call.