Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #69675

Re: Yet Another Switch-Case Syntax Proposal

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@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; 'else:': 0.03; 'explicitly': 0.05; 'context': 0.07; 'method.': 0.07; 'removes': 0.07; 'assigning': 0.09; 'clause': 0.09; 'contexts': 0.09; 'here?': 0.09; 'identifier': 0.09; 'statements': 0.09; 'terminates': 0.09; 'python': 0.11; 'def': 0.12; '":"': 0.16; '"if"': 0.16; '"lambda"': 0.16; '"python': 0.16; '"while"': 0.16; '(1,': 0.16; '*must*': 0.16; 'assignment.': 0.16; 'assignments': 0.16; 'assigns': 0.16; 'clauses': 0.16; 'colon.': 0.16; 'fond': 0.16; 'keyword,': 0.16; 'lambda': 0.16; 'mandate': 0.16; 'omitting': 0.16; 'ought': 0.16; 'pythonic': 0.16; 'statement.': 0.16; 'statements,': 0.16; 'subject:Case': 0.16; 'suite.': 0.16; 'syntax,': 0.16; 'true:': 0.16; 'two.': 0.16; 'exception': 0.16; 'language': 0.16; 'wrote:': 0.18; 'do.': 0.18; 'variable': 0.18; 'trying': 0.19; 'written': 0.21; 'this?': 0.23; 'char': 0.24; 'either.': 0.24; 'tells': 0.24; 'fairly': 0.24; 'switch': 0.26; 'this:': 0.26; 'pass': 0.26; 'least': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'am,': 0.29; 'generally': 0.29; "doesn't": 0.30; 'returned': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'follows': 0.31; 'forces': 0.31; 'header,': 0.31; 'second,': 0.31; 'username': 0.31; 'allows': 0.31; 'fri,': 0.33; 'could': 0.34; 'problem': 0.35; 'except': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'in:': 0.36; 'done': 0.36; 'doing': 0.36; 'two': 0.37; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'bad': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'even': 0.60; 'expression': 0.60; 'liked': 0.60; 'solve': 0.60; 'break': 0.61; 'entire': 0.61; 'skip:* 10': 0.61; 'first': 0.61; 'skip:n 10': 0.64; 'granted': 0.65; 'temporary': 0.65; 'effectively': 0.66; 'here': 0.66; 'between': 0.67; 'introduce': 0.78; 'mirroring': 0.84; 'or:': 0.84; 'capture': 0.91; 'subject:Proposal': 0.91
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 :content-type; bh=2DSBxs0y54KQ0PT5muuCKt3w5n0HCygGagiJOAw9b84=; b=N9Psna/vGhsGE8da6H+qUpTQ2KLo/txXU1kBgwFIw8E75QfCEukKEg0mW0iJbiHfy1 NUh9C6EMmaoYcne+8RefcPnOFFbLo86Tj5YmOnEm1jIDKSpwKwiFGn6oYFe9ofn+k9qa JsQ7LY2D9rkNJ59V6S+ML/X9gxOFvyO1bEYq9mYGKpfSouD0jlWonGbe67mCnoEEd0X4 Dppwi239yR6+6iBC91nFLboKP2mCSgT4twTnJTCl/Z7YzSVitYaDaETW9HA2o7KEjxZ/ BDEueP5EvZExMB7+wH6u/ZFLhORC+U2oZuUIfFiLDbWNVj+GND+M+wNVLXsTF4Wg7E2Z KyGA==
X-Received by 10.68.235.6 with SMTP id ui6mr16265428pbc.45.1396634151910; Fri, 04 Apr 2014 10:55:51 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <87ppkx6o4p.fsf@elektro.pacujo.net>
References <8084-1396540962-768613@sneakemail.com> <mailman.8852.1396550862.18130.python-list@python.org> <533e6e58$0$29993$c3e8da3$5496439d@news.astraweb.com> <87ioqpqvef.fsf@elektro.pacujo.net> <mailman.8894.1396627349.18130.python-list@python.org> <87ppkx6o4p.fsf@elektro.pacujo.net>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Fri, 4 Apr 2014 11:55:11 -0600
Subject Re: Yet Another Switch-Case Syntax Proposal
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
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.8897.1396634155.18130.python-list@python.org> (permalink)
Lines 114
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1396634155 news.xs4all.nl 2914 [2001:888:2000:d::a6]:34219
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:69675

Show key headers only | View raw


On Fri, Apr 4, 2014 at 10:44 AM, Marko Rauhamaa <marko@pacujo.net> wrote:
> Consider:
>
>     switch local_sabbath():        # bad
>     case (1, 2, 3) as sabbath:
>         ...

I'm not overly fond of that either.  That's why I liked the OP's
choice to put the first case in the switch statement.

> Now Python "framing" requires that you place something between the first
> ":" and "case":
>
>     switch local_sabbath():        # bad
>         pass
>     case (1, 2, 3) as sabbath:
>         ...

That's absurd.  Granted that omitting the pass doesn't match any
existing syntax, but that doesn't mean it *must* be done that way.

> Placing the expression after the colon terminates the first colon
> cleanly. Also, the "lambda" precedent allows an expression to follow a
> colon; both "lambda" and my "switch" mandate that the expression stay on
> the same line with the colon.

But in the case of the lambda *expression*, the following expression
is effectively the entire "suite" of the lambda "clause", mirroring
the form of a def statement.  If "local_sabbath()" is treated as the
suite of the switch *statement*, then "Python framing" tells us the
same construct could be written like this:

switch:
    local_sabbath()
case 1:
    ...

And if that's allowed, then why not this?

switch:
    if username == "ozzy":
        black_sabbath()
    else:
        local_sabbath()
case 1:
    ...

Or:

switch:
    for sabbath in list_of_sabbaths:
        sabbath
case 1:
    ...

Or even:

switch:
    pass
case 1:
    ...

The intent of the first two are fairly clear, but they're way out of
scope for a switch statement.  I don't even intuitively know what to
do with the last two.  The point here is that the switch expression
really ought to be a single expression contained in the switch header,
not a suite.


>> Second, "as" clauses are used in other contexts for local assignment.
>> What is the purpose of doing that here? How does this solve the
>> problem of explicitly denoting case multiplicity?
>
> The "as" clause follows the precedent of the "try/except" statement. It
> removes the occasional annoyance in C:
>
>    switch (next_char()) {
>    case '\n':
>    case '\r':
>        putchar(???);
>         :   :   :
>
> which forces you to introduce a temporary variable:

How is the target identifier of the "as" not a temporary variable?
Try/except statements and with statements use "as" keywords to capture
values that may not otherwise be available in scope for assignment.
Except assigns the exception instance that was just caught, and with
assigns whatever arbitrary object was returned by the context
manager's __enter__ method.  With switch there is no need for that;
just do this:

char = next_char()
switch next_char:
case in '\n\r':
    print(char)

Similarly there is no need for the "if" or "while" statements to
support an "as" keyword, as in:

while next_char() as char:
    print(char)

There are good reasons why the language generally does not allow
assignments in non-assignment statements, which is what your "as" is
trying to do.  The Pythonic way to write that loop is:

while True:
    char = next_char()
    if not char: break
    print(char)

Which is at least as ungraceful as assigning the switch variable
before the switch statement.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: Yet Another Switch-Case Syntax Proposal Ethan Furman <ethan@stoneleaf.us> - 2014-04-03 11:23 -0700
  Re: Yet Another Switch-Case Syntax Proposal Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-04-04 08:33 +0000
    Re: Yet Another Switch-Case Syntax Proposal Marko Rauhamaa <marko@pacujo.net> - 2014-04-04 12:46 +0300
      Re: Yet Another Switch-Case Syntax Proposal Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-04 10:02 -0600
        Re: Yet Another Switch-Case Syntax Proposal Marko Rauhamaa <marko@pacujo.net> - 2014-04-04 19:44 +0300
          Re: Yet Another Switch-Case Syntax Proposal Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-04 11:55 -0600

csiph-web