[Python-Dev] PEP 275: "Switching on Multiple Values", Rev 1.1

Paul Svensson paul-python@svensson.org
Tue, 13 Nov 2001 07:52:42 -0500 (EST)


On Tue, 13 Nov 2001, Skip Montanaro wrote:

>
>    mal> Syntax:
>
>    mal>     switch EXPR:
>    mal>         case CONSTANT:
>    mal>             SUITE  
>    mal>         case CONSTANT:
>    mal>             SUITE  
>    mal>         ...
>    mal>         else:
>    mal>             SUITE  
>
>    mal>     (modulo indentation variations)
>
>    mal>     The "else" part is optional. If no else part is given and none
>    mal>     of the defined cases matches, a ValueError is raised.
>
>Hmmm...  This doesn't jive well with current if statement semantics.  I can
>write 
>
>    if x == "first":
>       dofirst()
>
>and no ValueError is raised if x == "second".  Why should switch be any
>different?

A switch is a different beast, and should be considered afresh,
and not just as syntactic sugar for a (restricted) if-elif-else list.

However, in all other places Python allows an else clause,
a missing one is treated as ``else: pass'', and I don't see
any compelling reason why a switch should behave otherwise.

	/Paul