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


Groups > comp.lang.python > #69773

Re: Yet Another Switch-Case Syntax Proposal

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <elbarbun@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.023
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'example:': 0.03; 'else:': 0.03; 'syntax': 0.04; 'cc:addr:python-list': 0.11; 'be:': 0.16; 'etc...': 0.16; 'subject:Case': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'not,': 0.20; 'example': 0.22; 'cc:addr:python.org': 0.22; 'specify': 0.24; 'cc:2**0': 0.24; '&gt;': 0.26; 'equivalent': 0.26; 'suggested': 0.26; 'switch': 0.26; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'indentation': 0.31; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'false': 0.36; 'turn': 0.37; 'level': 0.37; 'skip:& 10': 0.38; 'skip:& 20': 0.39; 'ian': 0.60; "you'll": 0.62; 'email name :python-list': 0.65; 'reads': 0.68; 'useful.': 0.68; 'default': 0.69; 'day': 0.76; 'continue.': 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 :cc:content-type; bh=UyxjEzQr8PkvtvkNT+PM1J+ize3lGvimPCrVh7MjzA4=; b=JT9hhi3jpvYZX+js6l5S1gpxQ/y/lJ2k2mQT1H0sujtcH9tk8+xL8IEoZ/ZaRO8OmU Mf2ObgOY4DXhXhw6HR5H3mdOLQUkYu7PbalqiQw8TaMJhh/j/OfWFISlUIuQWPjexwtf msVlHcvtjO1Kppn4Z8JWzrwx40bxec+QXal+uOYl7u0dlPPmqtCDBGW02rYgUXPlqfgl /SLMzBfG7Q2TMNPpHKZamNAGQg/dpmR1z3sFETpi8BGu2Ljk6h0nztXaU1G1qmtDLHEn 1dXfBnZdKLLukunQUOtDBG1A5sJUFzjJjw3uD36FPzhch7jl+J8N2xrRNRSHVt1amBs4 mpiw==
X-Received by 10.236.76.105 with SMTP id a69mr5792698yhe.8.1396806626843; Sun, 06 Apr 2014 10:50:26 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <CALwzidmw0QbUbNw5PvwmHh+4ghguUhpEnY8TbBZYYyjZkpLBsw@mail.gmail.com>
References <8084-1396540962-768613@sneakemail.com> <CALwzidmw0QbUbNw5PvwmHh+4ghguUhpEnY8TbBZYYyjZkpLBsw@mail.gmail.com>
From "Marco S." <elbarbun@gmail.com>
Date Sun, 6 Apr 2014 19:49:45 +0200
Subject Re: Yet Another Switch-Case Syntax Proposal
To "Ian Kelly ian.g.kelly-at-gmail.com |python-list@python.org|" <dnl5yyr7ut@sneakemail.com>
Content-Type multipart/alternative; boundary=20cf303ea712d01ea704f6636221
Cc 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.8956.1396806635.18130.python-list@python.org> (permalink)
Lines 165
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1396806635 news.xs4all.nl 2865 [2001:888:2000:d::a6]:33163
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:69773

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

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.
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.
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... Your
example will turn into

briefing_days = ("Tue", "Thu")
festive_days = ("Sat", "Sun")

switch day case in briefing_days:
    lunch_time = datetime.time(11, 30)
    meeting_time = datetime.time(12, 30)
case not in briefing_days + festive_days:
    lunch_time = datetime.time(12)
    meeting_time = datetime.time(14)
case in festive_days:
    go_to_work = False
    day_type = "festive"
else:
    go_to_work = True
    day_type = "ferial"

The if-else equivalent will be:

if day in briefing_days:
    lunch_time = datetime.time(11, 30)
    meeting_time = datetime.time(12, 30)
if day not in briefing_days + festive_days:
    lunch_time = datetime.time(12)
    meeting_time = datetime.time(14)
if day in festive_days:
    go_to_work = False
    day_type = "festive"
else:
    go_to_work = True
    day_type = "ferial"


If you don't specify comp_operator, the default is ==. Example:

switch day_num case 1:
    day_str = "Monday"
elcase 2:
    day_str = "Thursday"
else:
    day_str = "etcetera"

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


Thread

Re: Yet Another Switch-Case Syntax Proposal "Marco S." <elbarbun@gmail.com> - 2014-04-06 19:49 +0200

csiph-web