Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #69672
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <jeanpierreda@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'python,': 0.02; 'programmer': 0.03; 'else:': 0.03; 'syntax': 0.04; 'value,': 0.04; 'elif': 0.05; 'remaining': 0.07; '"if': 0.09; 'default.': 0.09; 'etc).': 0.09; 'executed': 0.09; 'identifier': 0.09; 'pep': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'suggest': 0.14; 'random': 0.14; "wouldn't": 0.14; '":"': 0.16; '"if"': 0.16; '::=': 0.16; '["else"': 0.16; 'clause.': 0.16; 'ignoring': 0.16; 'iterable:': 0.16; 'statement.': 0.16; 'statements,': 0.16; 'subject:Case': 0.16; 'suite.': 0.16; 'suite;': 0.16; 'tuple,': 0.16; 'variables,': 0.16; 'exception': 0.16; 'weird': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'wed,': 0.18; 'bit': 0.19; 'written': 0.21; 'proposed': 0.22; 'cc:addr:python.org': 0.22; 'case.': 0.24; 'skip': 0.24; 'specify': 0.24; '(or': 0.24; 'cc:2**0': 0.24; 'solutions.': 0.26; 'switch': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'matching': 0.30; 'statement': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'code': 0.31; 'usually': 0.31; 'about.': 0.31; 'disabled': 0.31; 'url:python': 0.33; 'could': 0.34; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'consistent': 0.36; 'keyword': 0.36; 'yield': 0.36; 'url:listinfo': 0.36; 'next': 0.36; 'url:org': 0.36; 'should': 0.36; 'example,': 0.37; 'list': 0.37; 'desirable': 0.38; 'expect': 0.39; 'url:mail': 0.40; 'read': 0.60; 'chain': 0.60; 'expression': 0.60; 'tell': 0.60; 'break': 0.61; 'range': 0.61; 'simply': 0.61; 'first': 0.61; 'such': 0.63; 'skip:n 10': 0.64; 'decided': 0.64; 'more': 0.64; 'statement,': 0.68; 'default': 0.69; 'satisfied': 0.81; 'proposal:': 0.84; 'tricky': 0.84; 'subject:Proposal': 0.91; 'examine': 0.93; 'lucas': 0.93 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=vAyblHJpDzt1B/4CgrQpcxTn4fqk/LyfeC+y7oIUWl8=; b=V1Q1DhuRtEE3uq5W9fH3NXyHiToC3veiB/Y+TAFQNYMQTNjzcaBQSREPzNDIJblin0 QGBqsOmUa6WF2m3rNAaicMhXtfmoLvRKyO65+QCBZdnGBtmK6ore2FuBywwRCQSHPbnF hGsmZylXLp8qDzwyi01BYCUKlSIPisYQPffoWYq0kD8wHCx6o6GyKm1OnY8Js5WxxE4O Y9zhBhSN3bMEYGSMyK9QFpFdVwHe5MJlBu/kzPOcvKhuOrxq12tRdnoV0vvjPYEcu20J lCZdOByVomoeeL9ul6DV44uEiI1o5OLjs5CJgrg6aIpjJ9klj9NFlwomX9NCtOLQ4YBT q/eA== |
| X-Received | by 10.140.21.164 with SMTP id 33mr3484192qgl.97.1396628514424; Fri, 04 Apr 2014 09:21:54 -0700 (PDT) |
| MIME-Version | 1.0 |
| In-Reply-To | <12008-1396450399-453900@sneakemail.com> |
| References | <12008-1396450399-453900@sneakemail.com> |
| From | Devin Jeanpierre <jeanpierreda@gmail.com> |
| Date | Fri, 4 Apr 2014 09:21:14 -0700 |
| Subject | Re: Yet Another Switch-Case Syntax Proposal |
| To | Lucas Malor <3kywjyds5d@snkmail.com> |
| Content-Type | text/plain; charset=UTF-8 |
| Cc | "comp.lang.python" <python-list@python.org> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.8895.1396628517.18130.python-list@python.org> (permalink) |
| Lines | 62 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1396628517 news.xs4all.nl 2957 [2001:888:2000:d::a6]:47396 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:69672 |
Show key headers only | View raw
If one were to add switch into Python, wouldn't it be desirable to
make a pattern matching switch (think the "match" or "case" construct
from Haskell or ML)? Python currently has poor support for union/sum
types in general, not just enumerations. It feels weird to add better
support for enumerations while ignoring the broader use case.
If a switch statement gets added to Python and I still need to examine
ASTs by a long chain of if-elif and isinstance and so on I will cry.
-- Devin
On Wed, Apr 2, 2014 at 7:53 AM, Lucas Malor <3kywjyds5d@snkmail.com> wrote:
> Hi all. I would proposeto you all a switch-case syntax for Python. I already read PEP 3103 and I'm not completely satisfied by any of the proposed solutions. This is my proposal:
>
> switch_stmt ::= "switch" identifier "case" expression_list ":" suite
> ("case" expression_list ":" suite)*
> ["else" ":" suite]
>
> or, more simply:
>
>
>
> switch x case var1:
> ....
> case var2:
> ...
> case var3:
> ...
> else:
> ...
>
>
>
> Expression list should yield an iterable. The case suite will be executed if the variable of the identifier is a member of the iterable.
>
> For example, in a "switch x" statement, the code "case iterable: " is identical to "if x in iterable: " (or elif etc). So if you want to perform the same case block for more than one value, you have only to specify a tuple, a range etc.
> I would suggest to add an exception for non-iterable variables, so that you don't have to write "case var, : " but simply "case var: " and it will be identical to "if x == var: ". It's a bit tricky but the alternative is ugly.
>
> Fallthrough is disabled by default. The continue keyword cause to skip all the remaining current case suite. The next case will be checked. You can't use the continue keyword in the else clause.
>
> Some random remarks:
> 1. switch is on the same line of the first case. This will avoid any unpythonic syntaxes like:
> switch x
> case var1:
> ...
>
> or
>
> switch x:
> case var1:
> ...
>
> 2. Fallthrough is disabled by default because IMHO it's what non-programmers expect and that's what programmer usually needs more. I don't think a switch with such a syntax needs a break statement.
>
> 3. As an alternative, the continue statement could be written only at the end of a case suite; it will be less powerful and not consistent with the other compound statements, but it will improve readability.
>
> 4. I decided to not use already existing keyword like "if" or "in", since it will be misleading and problematic for syntax highlighters.
>
>
> Tell me what you think about.
> --
> https://mail.python.org/mailman/listinfo/python-list
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Yet Another Switch-Case Syntax Proposal Devin Jeanpierre <jeanpierreda@gmail.com> - 2014-04-04 09:21 -0700
csiph-web