Switch statement does not exist in Python. However you can simulate a switch statement using different techniques. We use this program as a training exercise. This lecture is optional, you will not learn anything new here. Just how to use what you have already learned previously.
Switch function:
In the next example we create a fake statement switch using two functions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #define a value holder function # => True def switch(value): switch.value=value return True #define matching case function # => True or False def case(*args): return any((arg == switch.value for arg in args)) # Switch example: print("Describe a number from range:") for n in range(0,10): print(n, end=",") print(flush=True) # Ask for a number and analyze x=input("n:") n=int(x) while switch(n): if case(0): print ("n is zero;") break if case(1, 4, 9): print ("n is a perfect square;") break if case(2): print ("n is an even number;") if case(2, 3, 5, 7): print ("n is a prime number;") break if case(6, 8): print ("n is an even number;") break print ("Only single-digit numbers are allowed.") pass # end of switch |
Example description
switch: In the example above I use one function “switch” with attribute “value” and one function “case” that return True or False if “switch.value” is one of arguments.
case: Is a function that receive a variable number of arguments. This function uses any(…), a Python function that returns True if any item in an iterable object is True, otherwise it returns False. If the iterable object is empty, the any() function will return False.
while: Using while loop will iterate one single time ant we can use break statement like a “switch” statement will do. This example demonstrate how using meaningful names for functions python language can be extended in interesting ways.
print: This example also demonstrate how to use print function to print numbers and avoid new line using optional parameter end=”,”.
Testing the program
Open this example live and run it: switch function
1 2 3 4 5 6 | Describe a number from range: 0,1,2,3,4,5,6,7,8,9, n:3 n is a prime number; Process finished with exit code 0 |
Switch class
Next example is similar, except we use a class to hold the value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #define a value holder class class switch(object): value = None def __new__(class_, value): class_.value = value return True # function to verify matching case # => True or False def case(*args): iterator = (arg == switch.value for arg in args) return any(iterator) # Switch example: for n in range(0,10): print(n,":", end="", flush=True) while switch(n): if case(0): print ("n is zero;") break if case(1, 4, 9): print ("n is a perfect square;") if case(2, 4, 6, 8): print ("n is an even number;") if case(2, 3, 5, 7): print ("n is a prime number;") break break pass # end switch pass # end for |
Homework: Run this example live: switch class
These two cases have demonstrate how to use a loop in a smarter way. The trick is to use significant names that “extend” the language. We exploit the fact that “switch” and “case” are not reserved keywords in Python, otherwise this would not work. In other situations you can create “frameworks” specific to your domain, that can be reused in multiple applications, extending the language with”domain specific” functions and classes.
Read next: Closure