Condition

Switch

Python

 

Switch

Python 2.7
status = 1

def case_0():
    print("Ready")

def case_1():
    print("Running")

def case_2():
    print("Done")

def default():
    print("Out of service")
    
try:
    switchOptions = {
        0 : case_0,
        1 : case_1,
        2 : case_2,
    }[status]()
except:
    default()

Python doesn't have Switch statements like other Programming Languages.

You can simply use chains of elif blocks, but this is not similar to how Switches work.

There are many opinions on how to mimic Switch behavior in Python.

Here we are using Dictionaries and Functions to mimic Switches.

This will print Running on Console.

 

Fallthrough

Python 2.7
status = 1

def case_0():
    print("Ready")

def case_12():
    print("Running or Done")

def default():
    print("Out of service")
    
try:
    switchOptions = {
        0 : case_0,
        1 : case_12,
        2 : case_12,
    }[status]()
except:
    default()

By following the previous card for mimicking Switches, it is possible to structure the code to have somewhat similar Fallthrough behavior like other Programming Languages.

Here if the status is 1 or 2, it will print Running or Done on Console.