[Python-Dev] switch-based programming in Python

Skip Montanaro skip@pobox.com (Skip Montanaro)
Fri, 9 Nov 2001 13:45:38 +0100


    >> switch EXPR:
    >>   case CONSTANT:
    >>     [suite]
    >>   case CONSTANT:
    >>     [suite]
    >>   else:
    >>     [suite]

    >> To my mind the cases are logically a subordinate part of the switch
    >> statement, and the indentation should reflect that.

    Thomas> Hmm. Perhaps. I'm not entirely convinced but it's not big an
    Thomas> issue.

Well, if nothing else, I think python-mode (and maybe other Python-aware
editors?) would have to make switch a special case, because it would be the
only statement with a colon at the end that *didn't* indent its subordinate
clauses.

    >> * Multiple values in a case

    >> CONSTANT, CONSTANT, ..., CONSTANT:

    Thomas> Meaning what ? Any one of them ? That would solve one part of
    Thomas> the fallthrough problem, but would require tuple-constants to be
    Thomas> parenthesised.  It's probably the most pythonic solution,
    Thomas> though.

We already have some places where to use tuples you have to parenthesize
them.  Perhaps this is another case of that.  When unparenthesized, it
represents a series of alternatives.  When it does have parens it's a tuple:

    switch point:
        if (0,0):
            do_origin()
        if (10,10):
            do_corner()
        if None:
            do_invalid()
        else:
            do_general(point)

Skip