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

Andrew Barnert abarnert at yahoo.com
Thu Apr 17 23:41:56 CEST 2014


On Apr 17, 2014, at 12:14, "Lucas Malor" <7vsfeu4pxg at snkmail.com> wrote:

> switch_stmt ::=  "switch" switch_expr "case" case_expr ":" suite
>    ("case" | "elcase" case_expr ":" suite)*
>    ["else" ":" suite]
> switch_expr ::= expression
> case_expr ::= expression_list

Any reason to use the keywords that only exist in C-family languages when the functionality isn't actually like C?

> - if case_expr is a tuple, the case suite will be executed if switch_expr is a member of the tuple

So there's no way to switch on a tuple?

> - if case_expr is not a tuple, the case suite will be executed if switch_expr == case_expr
> - if a case_expr is checked, any subsequent elcase statements are skipped, and the next case statement is performed, of there's one. This is completely identical to if - elif.

While you can figure out what this means with a bit of work, it seems to add a lot of conceptual complexity, and I can't think of any other language that does this.

Meanwhile, if you're trying to design a better and more powerful switch statement than other languages have, why not consider features that other languages do have, like pattern matching cases? Maybe it would be too hard to implement, or would conflict with another part of the feature, or just wouldn't fit in with Python, but if so, a switch proposal should try to explain that.


More information about the Python-ideas mailing list