PDA

View Full Version : Python switch



bobman321123
July 6th, 2012, 09:11 PM
How would I convert the following block from C++ into Python?


cin >> input;
switch(input)
{
case 'testing': anotherVariable=4;
break;
case 'etc': thisVariable=87;
break;
default: break;
}

Barrucadu
July 6th, 2012, 09:20 PM
http://docs.python.org/faq/design.html#why-isn-t-there-a-switch-or-case-statement-in-python

MadCow108
July 6th, 2012, 09:20 PM
any switch statement can be converted to if..elif..else



if input == 'testing':
pass
elif input == 'etc':
pass
else:
pass


but you don't have a compiler to optimize that to a jump table, you would have to do that yourself
luckily its easy with python dictionaries.

also note that input is a builtin function in python3, its better not to shadow it.

bobman321123
July 6th, 2012, 09:22 PM
any switch statement can be converted to if..elif..else



if input == 'testing':
pass
elif input == 'etc':
pass
else:
pass
but you don't have a compiler to optimize that to a jump table, you would have to do that yourself
luckily its easy with python dictionaries.

also note that input is a builtin function in python3, its better not to shadow it.

Is there any way you could do the above with a dict? I can't figure out how to set a variable value in a dict.

MadCow108
July 6th, 2012, 09:26 PM
just return the value you want in your function (or lambda) and assign it to what you want:



jumptable = dict(testing=lambda: 187)
thisvariable = jumptable.get(input, lambda : 'default')()

edit:
just saw you assign to different variables in switch (which seems like a bad design, it may leave stuff uninitialized, its better to branch of to different functional paths)
it is of course possible, but it probably gets ugly
do you really need a jumptable for this?
the python way favors readability over performance

one (ugly) way I can think of right now would look like this:

jumptable = dict(testing=lambda: ('var1',187), etc=lambda: ('var2','bla'))
res = jumptable["testing"]()
locals()[res[0]] = res[1]