ssiyad's wiki



Switch

Efficient illustration of (non-existing) Switches in python

def switch(choice: str):
    return {
        "choice1": "foo",
        "choice2": "bar",
    }[choice]

Dictionary values are evaluated before selection so NameError and other exceptions may occur.