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


Groups > comp.lang.python > #69615

Re: Yet Another Switch-Case Syntax Proposal

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; 'else:': 0.03; 'subsequent': 0.05; 'skip:u 30': 0.07; 'executes': 0.09; 'statements': 0.09; 'cc:addr:python-list': 0.11; 'file;': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'instead:': 0.16; 'subject:Case': 0.16; 'terribly': 0.16; 'appropriate': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'obviously': 0.18; 'else,': 0.19; 'written': 0.21; 'seems': 0.21; 'cc:addr:python.org': 0.22; 'form:': 0.24; 'cc:2**0': 0.24; 'switch': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'am,': 0.29; 'ideal': 0.29; 'raise': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'that.': 0.31; 'indentation': 0.31; 'probably': 0.32; 'languages': 0.32; 'fri,': 0.33; "i'd": 0.34; 'skip:u 20': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'really': 0.36; 'executing': 0.36; 'false': 0.36; 'useful': 0.36; 'should': 0.36; 'level': 0.37; 'implement': 0.38; 'moving': 0.39; 'sure': 0.39; 'how': 0.40; 'catch': 0.60; 'ian': 0.60; 'break': 0.61; 'more': 0.64; 'bottom': 0.67; 'reads': 0.68; 'day': 0.76; 'subject:Proposal': 0.91; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=U1zcYDNhWEVtPiibVervXJeK4cpPNpzV3SEt86V9KVQ=; b=arUlFfvxlDuMMjmWGJvjjMiAsWdwEeiu16bJ9GS6yYMDEM3KrIC51D+rSYcInD8pip D+dQtGwl8202iUMbl1i100vGjjFJi0fWPvs5KxSG10HNK/W0jkw/b58DWu2xBN0nw9Ib tJUYQmdaW6vXNoO4Rqd3T+ZzYN1KVendyImPOlwxOBXCbviq/SxmD/q+rOLdeyYs+iJR 7zbeQs3Q2KIXLvj2+pbRnXNgbeNxcWfxMqKOhQVSFE2oLC1FuzKs+PK/uhO80nzSw7MK jMkEUQMsfrL3nupl6BsCMMmR0GKn3IgPocFkPRhTp1sVh2chHkdLNXaD4v+Nf9zK0z2r XGCw==
MIME-Version 1.0
X-Received by 10.66.142.201 with SMTP id ry9mr10638704pab.14.1396563641670; Thu, 03 Apr 2014 15:20:41 -0700 (PDT)
In-Reply-To <CALwzidmw0QbUbNw5PvwmHh+4ghguUhpEnY8TbBZYYyjZkpLBsw@mail.gmail.com>
References <8084-1396540962-768613@sneakemail.com> <CALwzidmw0QbUbNw5PvwmHh+4ghguUhpEnY8TbBZYYyjZkpLBsw@mail.gmail.com>
Date Fri, 4 Apr 2014 09:20:41 +1100
Subject Re: Yet Another Switch-Case Syntax Proposal
From Chris Angelico <rosuav@gmail.com>
Cc Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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.8860.1396563963.18130.python-list@python.org> (permalink)
Lines 65
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1396563963 news.xs4all.nl 2840 [2001:888:2000:d::a6]:53854
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:69615

Show key headers only | View raw


On Fri, Apr 4, 2014 at 5:12 AM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> Use this instead:
>
> switch day case in ("Mon", "Tue", "Wed", "Thu", "Fri"):
>     go_to_work = True
>     day_type = "ferial"
>     if day in ("Tue", "Thu"):
>         lunch_time = datetime.time(11, 30)
>         meeting_time = datetime.time(12, 30)
>     else:
>         lunch_time = datetime.time(12)
>         meeting_time = datetime.time(14)
> case in ("Sat", "Sun"):
>     go_to_work = False
>     day_type = "festive"
>
> You get an extra level of indentation this way, but it reads less like
> spaghetti code.

Still not an ideal demonstration of fall-through. Here's a much more
useful form:

switch get("version") case 0:
    commands_to_get_to_version_1
case 1:
    commands_to_get_to_version_2
case 2:
    commands_to_get_to_version_3

    # Version 3 is current.
    set("version", 3)
case 3:
    break
else:
    raise UnknownVersionError("Unexpected version!")

With fall-through, you don't need a loop around that. You jump to the
appropriate point and start executing code until you get to the bottom
(apart from the else, which obviously should never happen).

To implement this in current Python, I'd probably do all the
comparisons as inequalities:

v = get("version")
if v<0:
    raise UnknownVersionError("Version is below zero!")
if v<1:
    commands_to_get_to_version_1
if v<2:
    commands_to_get_to_version_2

    # Version 3 is current.
    set("version", 3)
if v>3:
    raise UnknownVersionError("Version is moving backward!")

Which means this isn't really a terribly compelling use-case; but I
think it's a better one than overlapping ranges. Fall-through in
C-like languages completely ignores the case labels on subsequent
sections, and executes them because of their position in the file; I'm
not sure how it's looking with the current proposals, but it seems the
case statements would have to be written to catch the values from
above.

ChrisA

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


Thread

Re: Yet Another Switch-Case Syntax Proposal Chris Angelico <rosuav@gmail.com> - 2014-04-04 09:20 +1100

csiph-web