[Python-Dev] switch-based programming in Python

Paul Svensson paul@svensson.org
Thu, 15 Nov 2001 06:55:39 -0500 (EST)


On Thu, 15 Nov 2001, Greg Ewing wrote:

>Donald Beaudry <donb@abinitio.com>:
>
>> 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?
>
>No, please, don't try to make it work like the loops do!  By the
>principle of least astonishment when coming from other languages, that
>would be a nightmare.

Yes, it should work exactly like the loops do;
it's just Donald who's confusing how `else' clauses on loops work,
There's nothing "normal" about running to the end of a loop.

The `else' clause is entered when the (explicit or implicit) test
of the main statement fails.
This is how it works with `if', `except', and loops.
Anything else would be confusing and error-prone.

Thus, in the below code, one and only one of the three functions is called.

    select flip_eggs:
        when 1:
            sunny_side_up()
        when 2:
            over_easy()
    else:
        scrambled()


		/Paul