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


Groups > comp.lang.python > #70064

Re: Yet Another Switch-Case Syntax Proposal

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!us.feeder.erje.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <3kywjyds5d@snkmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.015
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'example:': 0.03; 'else:': 0.03; 'element': 0.07; 'executed': 0.09; 'expression:': 0.09; 'solution,': 0.09; '":"': 0.16; '::=': 0.16; '["else"': 0.16; 'subject:Case': 0.16; 'tuple': 0.16; 'tuple,': 0.16; 'tuple.': 0.16; 'wrote:': 0.18; 'basically': 0.19; 'simpler': 0.24; 'switched': 0.24; 'switch': 0.26; 'chris': 0.29; 'about.': 0.31; 'another': 0.32; 'equal': 0.35; 'but': 0.35; 'false': 0.36; 'too': 0.37; 'two': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'expression': 0.60; 'received:unknown': 0.61; 'length': 0.61; 'simply': 0.61; 'first': 0.61; 'card': 0.63; 'day': 0.76; 'received:38': 0.81; 'tricky': 0.84; 'proposal,': 0.91; 'subject:Proposal': 0.91
Date Thu, 10 Apr 2014 21:15:44 +0000
To python-list@python.org
Subject Re: Yet Another Switch-Case Syntax Proposal
MIME-Version 1.0
Content-Type text/plain; charset=utf-8
From "Lucas Malor" <3kywjyds5d@snkmail.com>
X-Mailer Perl5 Mail::Internet v
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.9150.1397164552.18130.python-list@python.org> (permalink)
Lines 55
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1397164552 news.xs4all.nl 2926 [2001:888:2000:d::a6]:32868
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:70064

Show key headers only | View raw


On 6 April 2014 20:07, Chris Angelico rosuav-at-gmail.com |python-list@python.org| <d7v3ztas9t@sneakemail.com> wrote:
> 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

I like this solution, but I tought about a simpler one. Basically it's my first proposal with two modifications:
1. case / elcase instead of continue, as before
2. if the case expression is a *tuple* (and not any one iterable), case suite is executed if the switched expression is an element of the tuple.

If you want to match exactly a tuple, you have simply to put it into another tuple of length 1. Tricky but too much rare to care about.
To recap:

switch_stmt ::=  "switch" expression "case" expression_list ":" suite
    ("case" | "elcase" expression_list ":" suite)*
    ["else" ":" suite]
1. if expression_list is a tuple, the case suite will be executed if the switch expression is a member of the tuple.
2. if expression_list is not a tuple, the case suite will be executed if the switch expression is equal
Example:

briefing_days = ("Tue", "Thu")
normal_days = ("Mon", "Wed", "Fri")

switch day normal_days + briefing_days:
    go_to_work = True
    day_type = "weekday"
case normal_days:

    lunch_time = datetime.time(12)
    meeting_time = datetime.time(14)
elcase briefing_days:

    lunch_time = datetime.time(11, 30)
    meeting_time = datetime.time(12, 30)
else:

    go_to_work = False
    day_type = "festive"
    lunch_time = None
    meeting_time =None

Another example:
switch tarot case 0:
    card = "Fool"
case 1:
    card = "Alan Moore"
case 2:
    card = "High Priestess"
etc.

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


Thread

Re: Yet Another Switch-Case Syntax Proposal "Lucas Malor" <3kywjyds5d@snkmail.com> - 2014-04-10 21:15 +0000

csiph-web