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


Groups > comp.lang.python > #69783

Re: Yet Another Switch-Case Syntax Proposal

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.kamp.net!newsfeed.kamp.net!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.81.MISMATCH!newsfeed.xs4all.nl!newsfeed2.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.078
X-Spam-Evidence '*H*': 0.85; '*S*': 0.00; 'else:': 0.03; 'syntax': 0.04; 'that?': 0.05; 'etc...': 0.16; 'subject:Case': 0.16; 'syntax,': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'dependent': 0.19; 'not,': 0.20; 'print': 0.22; 'regardless': 0.24; 'suggested': 0.26; 'switch': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'indentation': 0.31; 'could': 0.34; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'false': 0.36; 'surely': 0.36; 'possible': 0.36; 'similar': 0.36; 'level': 0.37; 'to:addr :python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; 'chain': 0.60; 'ian': 0.60; 'most': 0.60; "you'll": 0.62; 'reads': 0.68; 'useful.': 0.68; 'day': 0.76; 'continue.': 0.91; 'subject:Proposal': 0.91; 'lucas': 0.93; 'suited': 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=DgUPF+xrmkmKMTi+0G0/9OUiNshjIJqpTWiad83gnkY=; b=ix0IA4cklDT/0xF3jpdDSwIR66hwEKs82fh7+4wTkNkK2UJOglXDkE3drc3bA6GymF fxQyMpOrHUxpdFDjLYarJBf/j6dDRzRHD0c1QEGq1fxt/mvc8GcnfiubtxhPf954bFEE dC9VxGpgB1C3ScWCrL9jbu0OVQTPI6ULbWqoTccsBvRmIw4rhnuJWOj+ifcKLoMSsGHz JX/7LubBjhTM8NgFzWbDazHTfmdvymqcWteijyANjedP6tISTzmbW65WUJFxOzwbvk4V QrESGG69i78RWtqYO8ypd8gnbbWqvuWwpvp/yOVVFcgoXBDuy8F6W0xIWmGj0m1ro2Gu nKNA==
X-Received by 10.68.228.1 with SMTP id se1mr26935378pbc.43.1396817787807; Sun, 06 Apr 2014 13:56:27 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <11366-1396806627-75788@sneakemail.com>
References <8084-1396540962-768613@sneakemail.com> <CALwzidmw0QbUbNw5PvwmHh+4ghguUhpEnY8TbBZYYyjZkpLBsw@mail.gmail.com> <11366-1396806627-75788@sneakemail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Sun, 6 Apr 2014 14:55:47 -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.8963.1396817791.18130.python-list@python.org> (permalink)
Lines 55
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1396817791 news.xs4all.nl 2977 [2001:888:2000:d::a6]:42491
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:69783

Show key headers only | View raw


On Sun, Apr 6, 2014 at 11:49 AM, Lucas Malor <3kywjyds5d@snkmail.com> wrote:
> On 3 April 2014 20:12, Ian Kelly ian.g.kelly-at-gmail.com
> |python-list@python.org| <dnl5yyr7ut@sneakemail.com> wrote:
>> Use this instead [of continue]:
>
>>
>> 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.
>
>
> Well, if you have to add an if-else to your switch-case, it means
> switch-case syntax is not so useful.

I agree; the above is better suited to be an if.

> An alternative is to imitate elif, so you'll have elcase. This way we don't
> need continue. Since I do not like elcasein, the best solution is to do as
> suggested by many of you, so case in instead of casein.

So if I'm understanding your intention correctly, "case" means to
check this case regardless of whether any preceding case was matched,
and "elcase" means to check this case only if the most recent "case"
and its dependent "elcases" preceding this one were not matched.

switch "Mon" case in ("Tue", "Thu"):
    print(1)
elcase in ("Mon", "Wed", "Fri"):
    print(2)
case in ("Sat", "Sun"):
    print(3)
elcase in ("Mon", "Tue", "Wed", "Thu", "Fri"):
    print(4)

will print 2 and 4, correct?

> But if you can write case in, why you can't write "case" comp_operator in
> general? With this syntax you can do also case is not, case > etc...

At this point I see no advantage in adding this syntax.  It is so
similar to an if-elif chain that surely any optimization that could be
applied would be equally possible if the if-elif syntax, so why not
just use that?

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-06 14:55 -0600

csiph-web