Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed4a.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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'operator': 0.03; 'clause': 0.09; 'expression:': 0.09; 'false.': 0.09; 'parsing': 0.09; 'cc:addr:python-list': 0.11; '(without': 0.16; 'be:': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'marco': 0.16; 'oddity': 0.16; 'slight': 0.16; 'subject:Case': 0.16; 'unlikely': 0.16; 'language': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'basically': 0.19; 'normally': 0.19; '(in': 0.22; 'cc:addr:python.org': 0.22; 'adds': 0.24; 'simpler': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'equivalent': 0.26; 'switch': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; 'assumes': 0.31; 'block,': 0.31; 'comparison': 0.31; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'edge': 0.36; 'false': 0.36; 'keyword': 0.36; 'done': 0.36; 'previous': 0.38; 'explain': 0.39; 'does': 0.39; 'enough': 0.39; 'either': 0.39; 'easy': 0.60; 'expression': 0.60; 'skip:n 10': 0.64; 'effectively': 0.66; 'day': 0.76; "'case'": 0.84; 'opens': 0.91; 'proposal,': 0.91; '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=TVA/vonkl7m0S/bP1Sy+QEe0T4O8Tg4vnCcWdwJcbxs=; b=dPa1PEHB9td1NkVuu5FWUlKAn8lWzCfZP/RFX+hnK7MRWfIRmn7EV4vtB7+JDS13Jf AO2ILTFFFNpGBJs1XHU31kMvkOHdUjw3m4UtOls5fnaLsEPMYfnlBmkneh9ZtpB1iX3/ v8inE+rcvnGzUTXx/Jik5eLyuw0GdC0X+06rE6iTyjC47BXI/YfZHqCsSiRc8Z85BHUA 2zw9CGTEMKjWS+ivBGVd+YNs4lXXHqUQkK/hV0p5iIt0N7nQOsF4tRBraj/f4UuhtqCv p5L9md8DN0jcXdH1olKDaVEk0XPaUEdCwTDb53ywXKvtpdZ3VyQwVbw0nKDW2TgbynD+ tsBg== MIME-Version: 1.0 X-Received: by 10.68.226.197 with SMTP id ru5mr26468549pbc.77.1396807622202; Sun, 06 Apr 2014 11:07:02 -0700 (PDT) In-Reply-To: References: <8084-1396540962-768613@sneakemail.com> Date: Mon, 7 Apr 2014 04:07:02 +1000 Subject: Re: Yet Another Switch-Case Syntax Proposal From: Chris Angelico Cc: Python 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 77 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1396807975 news.xs4all.nl 2871 [2001:888:2000:d::a6]:42027 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69777 On Mon, Apr 7, 2014 at 3:49 AM, Marco S. wrote: > 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" Here's a simpler form of the proposal, which might cover what you need. It's basically a short-hand if/elif tree. case expression comp_op expression: suite case [comp_op] expression: suite ... else: suite This has a slight oddity of parsing (in that an expression can normally have a comparison in it); if you really want to use the result of a comparison inside a case block, you'd have to parenthesize it. But it's easy enough to explain to a human. case day 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" A case statement that opens with a comparison operator takes the value from the previous case (without re-evaluating it); a case statement that lacks a comparison altogether assumes == and does the above. In either case (pardon the pun), the check will be done only if the preceding case was false. An 'else' clause is effectively equivalent to a 'case' that's always true. Adds only one keyword to the language ("switch" is gone), and adds an edge case to parsing that's unlikely to come up in non-contrived code. ChrisA