The content of a test file:
from __experimental__ import switch_statement
def example(n):
result = ''
switch n:
case 2:
result += '2 is even and '
case 3, 5, 7:
result += f'{n} is prime'
break
case 0: pass
case 1:
pass
case 4, 6, 8, 9:
result = f'{n} is not prime'
break
default:
result = f'{n} is not a single digit integer'
return result
for i in range(11):
print(example(i))
Trying it out
$ python -m experimental test_switch
0 is not prime
1 is not prime
2 is even and 2 is prime
3 is prime
4 is not prime
5 is prime
6 is not prime
7 is prime
8 is not prime
9 is not prime
10 is not a single digit integer
Just having fun ... Please, do not even think of using this for serious work.
Nice! Beings me back to the C days. Michael Kennedy who does one of the Python podcasts did something similar. He has a write up over at github.
ReplyDeletehttps://github.com/mikeckennedy/python-switch
-Jack