[Python-Dev] Switch statement

Steven Bethard steven.bethard at gmail.com
Fri Jun 23 19:52:07 CEST 2006

[delurking in response to the first really decisive message in the thread] ;-)

On 6/22/06, Guido van Rossum <guido at python.org> wrote:
> (1) An expression of the form 'static' <atom> has the semantics of
> evaluating the atom at the same time as the nearest surrounding
> function definition.

FWIW, +1.  This is clear and useful.  And code like the following
could be fixed by simply adding a "static" before the "i", instead of
adding default arguments::

    funcs = []
    for i in xrange(10):
        def f():
            return i

> If there is no surrounding function definition,
> 'static' is a no-op and the expression is evaluated every time.
> [Alternative 1: this is an error]

+1 on the error.  Function definition time doesn't really make sense
for modules.  Only +0 on allowing only compile-time constants, since
it would be a little harder to explain.  I guess you'd want to tell
people something like "think of a module as being a function that is
defined at compile time and called on import".

> (2) All case expressions in a switch have an implied 'static'.

+1.  You already have to understand that switch statements are
evaluated at function definition time, so the 'static' does seem a bit

> (3) A switch is implemented using a dict which is precomputed at the
> same time its static expressions are precomputed. The switch
> expression must be hashable. Overlap between different cases will
> raise an exception at precomputation time.

+1.  What a wonderful, simple explanation. =)

> Independent from this, I wonder if we also need static names of the form
>   static <name> = <expression>
> which would be similar to
>   <name> = static (<expression>)
> but also prevents <name> from being assigned to elsewhere in the same scope.

-0.  I'm not sure how often this is really necessary.  I'd rather see
static expressions in Python 2.6, see how people use them, and then
decide whether or not static names are also needed.

> Also, I haven't heard a lot of thumbs up or down on the idea of using
>   case X:
> to indicate a single value and
>   case in S:
> to indicate a sequence of values.

+1.  This syntax seems pretty intuitive.

I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a
tiny blip on the distant coast of sanity.
        --- Bucky Katt, Get Fuzzy

More information about the Python-Dev mailing list