[Python-Dev] switch-based programming in Python

Martin v. Loewis martin@v.loewis.de
Thu, 8 Nov 2001 17:51:32 +0100


>     >> switch x:
>     >>   case 'foo':
>     >>     ...
>     >>   case 'bar':
>     >>     ...
>     >>   case 42:
>     >>     ...
> 
> If you restrict the case values to hashable literals do you need "case"?

I'm not going to draft a language extension here :-)

Syntactically, you don't need it - even if you allow arbitrary complex
expressions: you probably wouldn't allow any statements directly
nested into the switch. It may be that

switch x:
  if 'foo':
    ...
  elif 'bar':
    ...

is also acceptable, and doesn't need the new keyword.

> One new keyword would be easier than two for Guido to swallow...

You'd introduce it through a __future__ import, so it wouldn't matter
if it is one or two.

Regards,
Martin