Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: <3kywjyds5d@snkmail.com> X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.031 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'say,': 0.05; 'expressions': 0.07; 'nested': 0.07; 'string': 0.09; 'extends': 0.09; 'identifier': 0.09; 'pep': 0.09; 'python': 0.11; 'posted': 0.15; '"an': 0.16; '"elif"': 0.16; 'comma': 0.16; 'for,': 0.16; 'iterable': 0.16; 'iterables': 0.16; 'keyword.': 0.16; 'membership:': 0.16; 'separated': 0.16; 'string:': 0.16; 'subject:Case': 0.16; 'tempted': 0.16; 'tuple.': 0.16; 'unambiguous': 0.16; 'do.': 0.18; 'example': 0.22; 'instead.': 0.24; 'propose': 0.24; 'right.': 0.26; 'switch': 0.26; 'least': 0.26; 'testing': 0.29; 'chris': 0.29; 'raise': 0.29; "doesn't": 0.30; 'default,': 0.31; 'disable': 0.31; 'object.': 0.31; 'yields': 0.31; 'class': 0.32; 'interface': 0.32; 'another': 0.32; 'cases': 0.33; 'skip:_ 10': 0.34; 'problem': 0.35; "can't": 0.35; 'problem.': 0.35; 'test': 0.35; 'but': 0.35; 'there': 0.35; 'keyword': 0.36; 'should': 0.36; 'list': 0.37; 'list.': 0.37; 'implement': 0.38; 'thank': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'mailing': 0.39; 'read': 0.60; 'expression': 0.60; 'ian': 0.60; 'solve': 0.60; 'received:unknown': 0.61; 'hope': 0.61; 'introduced': 0.61; 'new': 0.61; 'simply': 0.61; "you're": 0.61; 'skip:n 10': 0.64; 'more': 0.64; 'linked': 0.65; 'within': 0.65; 'here': 0.66; 'reply': 0.66; 'useful.': 0.68; 'containing': 0.69; 'day': 0.76; 'introduce': 0.78; 'received:38': 0.81; 'abc': 0.84; 'commenting': 0.84; 'compare:': 0.84; 'proposal.': 0.84; 'subject:Proposal': 0.91 Date: Thu, 3 Apr 2014 16:02:42 +0000 To: python-list@python.org Subject: Re: Yet Another Switch-Case Syntax Proposal MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 From: "Lucas Malor" <3kywjyds5d@snkmail.com> X-Mailer: Perl5 Mail::Internet v X-Mailman-Approved-At: Thu, 03 Apr 2014 18:03:26 +0200 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 59 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1396541006 news.xs4all.nl 2975 [2001:888:2000:d::a6]:54554 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69592 Thank you for reading and commenting my proposal. Here are my replies: In reply to Chris Angelico: > I don't like the "iterable vs non-iterable" distinction. Compare: [...] > case "Test": # And there's your problem. Yes, I had already thought after I posted that testing against string it's a problem. But I think that the more practical and unambiguous solution is to introduce also "casein" as a new keyword. So if you want to test a string: switch mystring case "-h": print_usage() case "--version" print_version() If you want to test against membership: switch mychar casein "aeiou": mychar.vocal = True I prefer "casein" instead of "case in" for the same reason Python uses "elif" instead of "else if". In reply to Ian Kelly: > A more suitable place to propose this would be the python-ideas mailing list. You're right. I posted here because this list was linked by PEP 1. But now that I read more there's also python-ideas listed. Let me know if I have to continue there instead. > Why just just an identifier after the switch keyword instead of an expression? Because I wronged, it can be an expression_list as well. > An expression_list is one or more independent expressions separated by commas that > don't create a tuple. Well, py docs state "An expression list containing at least one comma yields a tuple". > __contains__ is not part of the interface for iterables For what I know there's not an Iterable interface. For example List simply extends Object. I hope that an ABC Iterable class will be introduced in a future. Anyway, I think that "switch x casein y" should simply raise a TypeError if y doesn't implement __contains__, like "x in y" do. > If we overload the continue keyword in this way, then a continue can't be used within the switch > to control a loop that the switch is nested within. Well, this is the same problem for, say, a for loop nested inside another for loop, and you can solve it with the same methods. > Instead of disabling fallthrough by default, why not disable it all together? I was tempted but there are cases in which it's useful. An example switch day casein ("Monday", "Thursday", "Wednesday", "Tuesday", "Friday"): gotowork = True continue casein ("Monday", "Thursday", "Wednesday", "Tuesday", "Friday"): daytype = "ferial" casein ("Saturday", "Sunday") daytype = "festive"