יום שבת, 11 במאי 2013

python has no switch or case or select

 

from Replacements for switch statement in python?

def f(x):
return {
'a': 1,
'b': 2,
}.get(x, 9) # 9 is default if x not found
or 

result = {
'a': lambda x: x * 5,
'b': lambda x: x + 7,
'c': lambda x: x - 2
}[value](x)
example of using the last code receipt:  
theSwitchArray = {
'a': lambda x: x * 5,
'b': lambda x: x + 7,
'c': lambda x: x - 2
}

result = theSwitchArray['b'](3)

print ( result )





אין תגובות:

הוסף רשומת תגובה