[Python-Dev] Switch statement

Roger Miller rogermiller at alum.mit.edu
Thu Jun 22 10:06:45 CEST 2006


Ka-Ping Yee wrote:
 > Hmm, this is rather nice.  I can imagine possible use cases for
 >
 >    switch x:
 >        case > 3: foo(x)
 >        case is y: spam(x)
 >        case == z: eggs(x)

Part of the readability advantage of a switch over an if/elif chain is 
the semantic parallelism, which would make me question mixing different 
tests in the same switch.  What if the operator moved into the switch 
header?

     switch x ==:
         case 1: foo(x)
	case 2, 3: bar(x)

     switch x in:
	case (1, 3, 5): do_odd(x)
	case (2, 4, 6): do_even(x)

"switch x:" could be equivalent to "switch x ==:", for the common case.

I've also been wondering whether the 'case' keyword is really necessary? 
  Would any ambiguities or other parsing problems arise if you wrote:

     switch x:
         1: foo(x)
	2: bar(x)

It is debatable whether this is more or less readable, but it seemed 
like an interesting question for the language lawyers.


More information about the Python-Dev mailing list