Is there no switch function in Python

Cameron Laird claird at
Fri Sep 10 17:08:04 CEST 2004

In article <roy-03E598.09352710092004 at>,
Roy Smith  <roy at> wrote:
>That being said, there are times when I miss switch.  For some kinds of 
>multi-branch logic, I think it expresses the meaning better than a 
>string of if's.  But it's hardly necessary.
>The real reason, IMHO, switch existed in C was because it would let the 
>compiler build very efficient jump tables for switches with many cases.  
>Imagine switching on the ascii value of a character and having a 128 
>different cases.  The compiler could build a 128 entry jump table and 
>the switch statement would compile down to a single machine instruction.

I probably miss switches as much as any Python coder.  Having
appropriately introduced the concept of "jump table", though,
it's only right that we mention how often Python uses dictionaries
in just such ways.

More information about the Python-list mailing list