AME AME - 1 year ago 69
Python Question

Does Python have an equivalent to 'switch'?

I am trying to check each index in an 8 digit binary string. If it is

then it is
otherwise its

Is there a more concise way to write this code with a switch-like feature.?

Answer Source

No it doesn't. In the Python core language, one of the rules is to only have one way to do something. The switch is redundant to:

if x == 1:
elif x == 5:
elif x == 10:

(without the fall-through, of course).

The switch was originally introduced as a compiler optimization for C. Modern compilers no longer need these hints to optimize this sort of logic statement.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download