Skip to content

Navigation Menu

Sign in
Sign up

Latest commit

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Python - Switch Case Simulation (C Like)

Python implementation of C Switch Case clause

How does it work?

Using python context manager and if blocks, we can simulate switch case by instantiating Switch class and calling its method __call__()

Using

Import Switch class from switch.py module

from switch import Switch

Create the cases block inside Switch instance context block

variable = 10
with Switch(variable) as case:
 if case(0):
 print('case 0')
 elif case(*range(1, 6)):
 print('case 1, 2, 3, 4 and 5')
 elif case(6, 7, 8):
 print('case 6, 7, and 8')
 elif case(9, 10):
 print('case 9 and 10')
 else:
 print('default')
# >> case 9 and 10

Suppressing exceptions

You can also handle exceptions that can occurs inside the switch block, without using try/except

variable = 11
mylist = list(range(10))
with Switch(variable, suppress=IndexError) as case:
 if case(*range(10)):
 print(mylist[variable]) # the same as print(mylist[case.value])
 else:
 print(mylist[variable])
 print('Index out of the range')

Without the suppress argument, it will raise the exception

variable = 11
mylist = list(range(10))
with Switch(variable) as case:
 if case(*range(10)):
 print(mylist[variable]) # the same as print(mylist[case.value])
 else:
 print(mylist[variable])
 print('Index out of the range')
# Traceback (most recent call last):
# File "~/tests.py", line 10, in <module>
# print(mylist[variable])
# IndexError: list index out of range

About

✔️ Python implementation of C Switch Case clause

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

AltStyle によって変換されたページ (->オリジナル) /