[Python-Dev] Switch statement

Phillip J. Eby pje at telecommunity.com
Mon Jun 19 18:49:07 CEST 2006

At 09:27 AM 6/19/2006 -0700, Raymond Hettinger wrote:
>Guido van Rossum wrote:
>>Um, is this dogma? Wouldn't a switch statement also be a welcome
>>addition to the readability? I haven't had the time to follow this
>>thread (still catching up on my Google 50%) but I'm not sure I agree
>>with the idea that a switch should only exist for speedup.
>A switch-statement offers only a modest readability improvement over 
>if-elif chains.  If a proposal introduces a switch-statement but doesn't 
>support fast dispatch, then it loses much of its appeal.

I would phrase that a lot differently.  A switch statement is *very* 
attractive for its readability.  The main problem is that if the most 
expressive way to do something in Python is also very slow -- i.e., people 
use it when they should be using a dictionary of functions -- then it adds 
to the "Python is slow" meme by attractive nuisance.  :)

Therefore, a switch statement should be made to perform at least as well as 
a dictionary of functions, or having it might actually be a bad thing.

In any case, we *can* make it perform as well as a dictionary of functions, 
and we can do it with or without another opcode.  What really needs to be 
decided on (i.e. by the BDFL) is the final syntax of the statement itself, 
and the semantics of evaluation time for the 'case' expressions, either at 
first execution of the switch statement, or at function definition time.

If explaining the evaluation time is too difficult, however, it might be an 
argument against the optimization.  But, I don't think that either 
first-use evaluation or definition-time evaluation are too hard to explain, 
since Python has both kinds of evaluation already.

More information about the Python-Dev mailing list