[Python-ideas] Yet Another Switch-Case Syntax Proposal

Stephen J. Turnbull stephen at xemacs.org
Sat Apr 26 17:07:56 CEST 2014


"Philipp A." <flying-sheep at web.de> wrote:

 > what y’all are missing about switch/case is that it’s *not*
 > necessarily a series of comparisons in most languages.

I don't think anybody missed this.  It's certainly been mentioned
several times.

Of course looking at a specific, full-featured example implementing
the idea of "switch on a value" using (and abusing, as pointed out by
Chris) existing Python features leads to the conclusion that such an
implementation

 > is of course completely ugly and unnecessarily complicated for
 > cases where multiple keys lead to the same branch.

I can't imagine anybody has missed this.  It is presumably one reason
why the OP is requesting new syntax.  This also has a more fundamental
flaw than "ugly" in many eyes: the need to define behavior *before*
defining the case.

The reason why people keep coming back to a series of comparisons
(actually more general: a series of branch on tests) is that a series
of comparisons is behaviorally equivalent to a switch.  Written in
appropriate style it's obvious that it is intended to have the effect
of a switch.  The code implementing a case is given "naturally" after
mentioning the case.  Given all that, it seems unnecessary to add new
syntax.

However, it's been mentioned that *if* we're going to have a case
statement, it would be nice to give it "pattern matching" abilities
such as the globbing of sh's 'case' or Haskell's signature matching.
That power might be enough to get some people to swing over to
supporting a syntax addition, it seems.

Starting from that summary of the thread, what are you trying to
argue?



More information about the Python-ideas mailing list