[Python-Dev] switch-based programming in Python

Donald Beaudry Donald Beaudry <donb@abinitio.com>
Wed, 14 Nov 2001 11:49:55 -0500


I was hoping someone would take the bait ;)

Paul Svensson <paul@svensson.org> wrote,
> On Tue, 13 Nov 2001, Greg Ewing wrote:
> 
> >Paul Svensson <paul@svensson.org>:
> >
> >> On Fri, 9 Nov 2001, Donald Beaudry wrote:
> >
> >> >    when EXPR:
> >> >        in CONSTANT_TUPLE:
> >> >            [suite]
> >> >    else:
> >> >         [suite]
> >
> >> you're absolutely right on the indentation of the "else".
> >
> >Really? To me, the else is just another branch, and should
> >be on the same level as all the others.
> 
> If you ignore how you get there, it's just another branch.
> But, how you get there is the greates distinction
> between the "else" branch and the other branches,
> and I think this should be emphasized, not ignored.
> 
> Compare how Python uses "else" not only with "if" statements,
> but also with for, while, and except.
> 
> Having the "else" indented with the "when" also makes it
> immediately obvious that there can't be more than one, 
> and it has to go at the end.
> 
> Besides, I find it visually more appealing.

I like the way it looks too, but the semantics are where it gets
sticky.  In a for or while, the else clause only gets executed when
the statement terminates "normally" (not due to a break).  Following
this model, one might expect the else clause associated with a 'when'
statement to be executed whenever a when's in caluse terminates
normally.  But what does "normally" mean in this context?  On the
other hand, if the else clause is to be like the default clause on a C
switch statment (what most would expect) I have to agree to indenting
the 'else' to the same level as the 'in'.  The potential confusion
here could be enough to argue against the use of else to mark the
default clause.  So, how about this:

        when EXPR:
            in CONSTANT-TUPLE:
               SUITE
            not in CONSTANT-TUPLE:
               SUITE
        else:
            SUITE

With this mess, the else can be as it is with the for and while
statements (though a definition of normal termination is still
needed) and 'not in ():'  could be used to mark the default
clause.


--
Donald Beaudry                                     Ab Initio Software Corp.
                                                   201 Spring Street
donb@init.com                                      Lexington, MA 02421
                      ...Will hack for sushi...