SiteMap  | About    

Switch-case statement in Python

Python doesn't have a switch/case statement because of Unsatisfactory Proposals . Nobody has been able to suggest an implementation that works well with Python's syntax and established coding style. There have been many proposals, some of which you can see in PEP 3103 -- A Switch/Case Statement .

Most programming languages have switch/case because they don't have proper mapping constructs. You cannot map a value to a function, that's why they have it. But in Python, you can easily have a mapping table(dict) where a certain value maps to a certain function. Python functions are first class values, you can use the functions as the values of the dictionary get(key[, default]) method. Performance-wise, the Python dictionary lookup will almost certainly be more efficient than any solution you can rig yourself.

In the example above, instead of values 1, 2 you can have functions, classes, lists, dicts absolutely anything you want.

example (C) 2018    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.