[Python-Dev] Simple Switch statement

Ka-Ping Yee python-dev at zesty.ca
Sun Jun 25 23:35:34 CEST 2006

On Sun, 25 Jun 2006, Guido van Rossum wrote:
> In your eagerness to
> rule out surprises, you're creating the biggest surprise of all: the
> restriction to literals is certainly a surprise!

I disagree.  Perhaps what we mean by "surprise" is different.  In
Raymond's design, there is a simple rule for what's allowed in a case.
The whole statement can be described in very few words:

    Each case is a literal integer, string, or tuple of integers
    or strings.  Execution jumps to the first case that matches the
    value of the switch expression (or to 'default' if no match).

That's it.  The simpler the rule, the less surprising it is.  It would
take a lot more words to explain the behaviour of something like Nick's
'once'-based proposal.  Here's an attempt:

    Each case consists of an arbitrary expression, but the expression
    may not refer to any local variables or arguments of the immediately
    surrounding function definition.  The value of each case is computed
    when the surrounding function definition is compiled.  If any two
    cases have the same value, an exception is thrown at compile time.
    At runtime, execution jumps to the case whose previously fixed value
    matches the value of the switch expression (or to 'default' if no

Not only is that longer to describe, it's also more difficult for a
beginning programmer to understand, since it requires knowing when
different parts of a program are compiled (and what happens if the
same part is compiled more than once).

-- ?!ng

More information about the Python-Dev mailing list