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: 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; '>': 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: References: <8084-1396540962-768613@sneakemail.com> From: "Marco S." 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|" Content-Type: multipart/alternative; boundary=20cf303ea712d01ea704f6636221 Cc: Python X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --20cf303ea712d01ea704f6636221 Content-Type: text/plain; charset=UTF-8 On 3 April 2014 20:12, Ian Kelly ian.g.kelly-at-gmail.com | python-list@python.org| 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" --20cf303ea712d01ea704f6636221 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
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", &qu= ot;Tue", "Wed", "Thu", "Fri"):
> = =C2=A0 =C2=A0 go_to_work =3D True
> =C2=A0 =C2=A0 day_type =3D "ferial"
> =C2=A0 =C2=A0 if= day in ("Tue", "Thu"):
> =C2=A0 =C2=A0 =C2=A0 = =C2=A0 lunch_time =3D datetime.time(11, 30)
> =C2=A0 =C2=A0 =C2=A0 = =C2=A0 meeting_time =3D datetime.time(12, 30)
> =C2=A0 =C2=A0 else: > =C2=A0 =C2=A0 =C2=A0 =C2=A0 lunch_time =3D datetime.time(12)
> = =C2=A0 =C2=A0 =C2=A0 =C2=A0 meeting_time =3D datetime.time(14)
> case= in ("Sat", "Sun"):
> =C2=A0 =C2=A0 go_to_work = =3D False
> =C2=A0 =C2=A0 day_type =3D "festive"
>
> You get an extra level of indentation this way, but it reads l= ess 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 wa= y we don't need conti= nue. 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 c= ase in, why you can't write <= span style=3D"font-family:courier new,monospace">"case" comp_oper= ator in general? With this syntax you can do also c= ase is not, case > etc... Your exa= mple will turn into

briefin= g_days =3D ("Tue", "Thu")
festive_days =3D ("Sat&qu= ot;, "Sun")

switch day ca= se in briefing_days:
=C2=A0 =C2=A0 lunch_time =3D datetime.time(11, 30)<= br>=C2=A0 =C2=A0 meeting_time =3D datetime.time(12, 30)
case not in brie= fing_days + festive_days:
=C2=A0 =C2=A0 lunch_time =3D datetime.time(12)
=C2=A0 =C2=A0 meeting_tim= e =3D datetime.time(14)
case in festive_days:
=C2=A0 =C2=A0 go_to_wor= k =3D False
=C2=A0 =C2=A0 day_type =3D "festive"
else:
=C2=A0 =C2=A0 go_to_work =3D True
=C2=A0 =C2=A0 day_type =3D "feria= l"

The if-else equivalent will be:

<= div>if day in briefing_da= ys:
=C2=A0 =C2=A0 lunch_time =3D datetime.time(11, 30)
=C2=A0 =C2=A0 meeting_time =3D datetime.time(12, 30)
if day not in brief= ing_days + festive_days:
=C2=A0 =C2=A0 lunch_time =3D datetime.time(12)<= br>=C2=A0 =C2=A0 meeting_time =3D datetime.time(14)
if day in festive_da= ys:
=C2=A0 =C2=A0 go_to_work =3D False
=C2=A0 =C2=A0 day_type =3D "festive"
else:
=C2=A0 =C2=A0 go_to_wor= k =3D True
=C2=A0 =C2=A0 day_type =3D "ferial"

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

switch day= _num case 1:
=C2=A0=C2=A0=C2=A0 day_str =3D "Monday"
=
elcase 2:
=C2=A0=C2=A0=C2=A0 day_str =3D "Thursday"
else:
=C2=A0=C2=A0=C2=A0= day_str =3D "etcetera"


--20cf303ea712d01ea704f6636221--