Why doesn't Python have a switch enclosure?

I am new to Python. Please explain why python doesn't have a switch key?

Thank you very much

+6
source share
4 answers

We looked at it at some point, but having no way to declare named constants, there is no way to create an efficient jump table. So all we have left is syntactic sugar for something that we could do with if-elif-elif-else chains.

See PEP 275 and PEP 3103 for a full discussion.

, , , (, , if-elif-chain, getattr-based ).

+19

. . :

Python switch case?

TL; DR: ( getattr dict.get, if/elif) .

+3
def f(x):
    return {
        1 : 'output for case 1',
        2 : 'output for case 2',
        3 : 'output for case 3'
    }.get(x, 'default case')   

python, , ,

+1

Even if Java, C++, C#support them, I have never used switch statements, so I can perform the same job with chained operators if.

0
source

All Articles