Python Switch Case: 2 Alternative Implementations

Python doesn’t support switch-case statements. There was a proposal to introduce Python switch case statements in PEP-3103 but it was rejected because it doesn’t add too much value.

We can easily implement switch-case statements logic using the if-else-elif statements. However, we can implement switch-case like behavior in Python using custom code.


Implementing Switch-Case in Python

There are two ways to implement switch-case behavior in Python.

  1. Using Dictionary
  2. Dynamic Function

1. Python Switch Case Implementation using Dictionary

We can create a dictionary where the key will be the case and the value will be the result. We will call the dictionary get() method with a default value to implement the scenario when there is no matching case.

Let’s say we have a following if-else block.

print("Choices:\n 1 - Email\n 2 - SMS\n 3 - Push Notification")
x = input("Please enter your choice:\n")

x = int(x)

if x is 1:
    print('sending email')
elif x is 2:
    print('sending sms')
elif x is 3:
    print('sending push notification')
else:
    print('wrong choice')

Now, we want to convert it to switch-case behavior using the dictionary.

print("Choices:\n 1 - Email\n 2 - SMS\n 3 - Push Notification")
x = input("Please enter your choice:\n")

x = int(x)

choices_dict = {1: 'sending email', 2: 'sending sms', 3: 'sending push notification'}

print(choices_dict.get(x, 'wrong choice'))
Python Switch Case Implementation
Python Switch Case Implementation

2. Implementing Python Switch-Case with Dynamic Functions

The above implementation works with simple print statements. But, most of the time we execute some method in the if-else blocks and then the dictionary will not work as switch-case replacement.

Let’s say we have the following if-else block to perform some operation on the input integer.

import math

x = input("Please enter an integer:\n")
x = int(x)

print("Choices:\n 1 - Square\n 2 - Cube\n 3 - Square Root")
choice = input("Please enter your choice:\n")

choice = int(choice)

if choice is 1:
    print(math.pow(x, 2))
elif choice is 2:
    print(math.pow(x, 3))
elif choice is 3:
    print(math.sqrt(x))

We can create a dynamic method to implement switch-case like behavior.

import math

x = input("Please enter an integer:\n")
x = int(x)

print("Choices:\n 1 - Square\n 2 - Cube\n 3 - Square Root")
choice = input("Please enter your choice:\n")

choice = int(choice)

def switch_func(value, i):
    return {
        1: lambda val: math.pow(val, 2),
        2: lambda val: math.pow(val, 3),
        3: lambda val: math.sqrt(val),
    }.get(value)(i)


print(switch_func(choice, x))

Here the choice is mapped to the dictionary key and the input integer is mapped to the lambda function. Finally, the output of the lambda function is returned from the switch_func() call.


Conclusion

It’s good to experiment the implementation of switch-case statements. But, it’s very confusing and we can’t implement complex scenarios with multiple statements in the if-else block. So, stick to the plain old if-else statements and keep your code simple and easy to read.


References: