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


Groups > comp.lang.python > #69599

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!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.125
X-Spam-Level *
X-Spam-Evidence '*H*': 0.75; '*S*': 0.00; 'else:': 0.03; 'extends': 0.09; 'instead:': 0.16; 'intersection': 0.16; 'iterable': 0.16; 'iterables': 0.16; 'subject:Case': 0.16; 'suite.': 0.16; 'tempted': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'thu,': 0.19; '>>>': 0.22; 'example': 0.22; 'import': 0.22; 'switch': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'default,': 0.31; 'disable': 0.31; 'indentation': 0.31; 'object.': 0.31; 'class': 0.32; 'there.': 0.32; 'handled': 0.32; 'interface': 0.32; 'cases': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'false': 0.36; 'useful': 0.36; 'detail': 0.37; 'example,': 0.37; 'two': 0.37; 'list': 0.37; 'level': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'hope': 0.61; 'introduced': 0.61; 'simply': 0.61; 'first': 0.61; 'more': 0.64; 'determine': 0.67; 'reads': 0.68; 'useful.': 0.68; 'union': 0.69; 'day': 0.76; 'abc': 0.84; 'suites,': 0.84; 'same,': 0.91; 'subject:Proposal': 0.91; 'write:': 0.91; '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 :content-type; bh=AQdA52e7FwwdFKcfAYaKW862R+5pW2vLI9yKu92QCJg=; b=C+nZk/B7TB1aHb7OfMOo9Ofwp7cMuUhcaFE2t9gfg3DfQQMDqrbywpugNXhBwgayqX hy0lZH+iimmHeDEFuii7FrS0uxOVcsade/XV9ukTu4mBt63Cp2sDI21lL5oGBW35nQgR SY0A5vFKGkvzywy6sw83oqH2Ss/VasYU3trAmnYrs4TzHvGED33HQjKbNUAIrpB2UCKH 5HQe/yOXAphIi+7D7bBgy2+hG/ONDrf5rIv2ia1dsPMlFGaBdn0WCkCewkHV27Lfka/j oxuT5Nm+9Nh1/M4UCUyDAPIFKcLG5yzDNoW7QgQFCWk2R6EyJ/A60TUpdV2oS0mdPXhp QpqQ==
X-Received by 10.66.240.70 with SMTP id vy6mr9253845pac.80.1396548781953; Thu, 03 Apr 2014 11:13:01 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <8084-1396540962-768613@sneakemail.com>
References <8084-1396540962-768613@sneakemail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Thu, 3 Apr 2014 12:12:21 -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.8848.1396548791.18130.python-list@python.org> (permalink)
Lines 61
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1396548791 news.xs4all.nl 2915 [2001:888:2000:d::a6]:41681
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:69599

Show key headers only | View raw


On Thu, Apr 3, 2014 at 10:02 AM, Lucas Malor <3kywjyds5d@snkmail.com> wrote:
>> __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.

It already exists:

>>> import collections.abc
>>> isinstance([1,2,3], collections.abc.Iterable)
True

>> 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"

I don't see how it's useful there.  The first two cases are the same,
so just combine the code into one suite.  For overlapping cases where
the intersection needs to be handled by both suites, you can match
against the union and use conditionals to determine in more detail
which code to run.  For example, if you would write:

switch day case in ("Mon", "Wed", "Fri"):
    lunch_time = datetime.time(12)
    meeting_time = datetime.time(14)
    continue
case in ("Tue", "Thu"):
    lunch_time = datetime.time(11, 30)
    meeting_time = datetime.time(12, 30)
    continue
case in ("Mon", "Tue", "Wed", "Thu", "Fri"):
    go_to_work = True
    day_type = "ferial"
case in ("Sat", "Sun"):
    go_to_work = False
    day_type = "festive"

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.

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


Thread

Re: Yet Another Switch-Case Syntax Proposal Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-03 12:12 -0600

csiph-web