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


Groups > comp.lang.python > #69556

Re: Yet Another Switch-Case Syntax Proposal

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'value,': 0.04; 'elif': 0.05; 'matches': 0.07; 'nasty': 0.07; 'ugly': 0.07; 'string': 0.09; '"if': 0.09; 'etc).': 0.09; 'cc:addr:python-list': 0.11; 'language,': 0.12; 'suggest': 0.14; '"st"': 0.16; '"test"': 0.16; 'defined,': 0.16; 'dict': 0.16; 'dictionary,': 0.16; 'equality.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'imo,': 0.16; 'iterable:': 0.16; 'iterating': 0.16; 'magic': 0.16; 'sane': 0.16; 'subclasses': 0.16; 'subject:Case': 0.16; 'surprising': 0.16; 'tuple': 0.16; 'tuple,': 0.16; 'variables,': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'thu,': 0.19; 'examples': 0.20; 'cc:addr:python.org': 0.22; "aren't": 0.24; 'case.': 0.24; 'interpret': 0.24; 'skip:{ 20': 0.24; 'specify': 0.24; 'switched': 0.24; '(or': 0.24; 'cc:2**0': 0.24; 'define': 0.26; 'equivalent': 0.26; 'possibly': 0.26; 'second': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'specifically': 0.29; 'am,': 0.29; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'easier': 0.31; 'equality': 0.31; 'keys': 0.31; 'object.': 0.31; 'such.': 0.31; 'class': 0.32; 'option': 0.32; 'cases': 0.33; 'third': 0.33; 'skip:_ 10': 0.34; "can't": 0.35; 'except': 0.35; 'problem.': 0.35; 'case,': 0.35; 'done.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'described': 0.36; 'method': 0.36; 'example,': 0.37; 'checks': 0.38; 'anything': 0.39; 'expect': 0.39; 'called': 0.40; 'how': 0.40; 'ensure': 0.60; 'new': 0.61; 'range': 0.61; 'simply': 0.61; 'first': 0.61; "you've": 0.63; 'real': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'real-world': 0.68; 'statement,': 0.68; 'results': 0.69; 'overall': 0.69; 'special': 0.74; 'beautiful,': 0.84; 'compare:': 0.84; 'fat': 0.84; "it'd": 0.84; 'tricky': 0.84; 'usage.': 0.84; 'subject:Proposal': 0.91; 'to:none': 0.92; 'imagine': 0.93; 'lucas': 0.93
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=ifyEO+BL2t1z0aHAkxpa+46XvP004be7nDTmdSuEyLE=; b=vcadmf/o0t/j5PgILQIA71CdY3ksB/YiCzJaOjhyXeNh1cD3GN+IYy9tzQP0uz/bMn QqJa6EXJnrMkrJRNseQdzJvjAb3Z5WS3+s9kP2eX3Q8eF9sPZ0ioHsOoR6w04lp4HAOm Sn120C5xMT67nKe4V8+7XWPCA2JRmUAKOpNJbXJxTh/T7uHkBbHIHekW3MwZnhPMxTfv hCJBZE5EGG/EoeF7GhiE+RQtOSD1e6WmNO5pvPZUMzAQXImqI4lnjyjZ5MQ4GuS5+GDv pXGOup5bfJWNWnvkWhHUc+egEUwDnwzzFZ1JJJI6baXsNH+EEmzpwQRNwTPEPxZGN3AF FcAw==
MIME-Version 1.0
X-Received by 10.66.181.70 with SMTP id du6mr2747205pac.23.1396475447356; Wed, 02 Apr 2014 14:50:47 -0700 (PDT)
In-Reply-To <12008-1396450399-453900@sneakemail.com>
References <12008-1396450399-453900@sneakemail.com>
Date Thu, 3 Apr 2014 08:50:47 +1100
Subject Re: Yet Another Switch-Case Syntax Proposal
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
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 <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.8822.1396475455.18130.python-list@python.org> (permalink)
Lines 44
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1396475455 news.xs4all.nl 2851 [2001:888:2000:d::a6]:47469
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:69556

Show key headers only | View raw


On Thu, Apr 3, 2014 at 1:53 AM, Lucas Malor <3kywjyds5d@snkmail.com> wrote:
> For example, in a "switch x" statement, the code "case iterable: " is identical to "if x in iterable: " (or elif etc). So if you want to perform the same case block for more than one value, you have only to specify a tuple, a range etc.
> I would suggest to add an exception for non-iterable variables, so that you don't have to write "case var, : " but simply "case var: " and it will be identical to "if x == var: ". It's a bit tricky but the alternative is ugly.
>

This is sounding like a nasty special case. I'm ambivalent on the
overall proposal (want to see some real-world usage examples and how
they read), but I don't like the "iterable vs non-iterable"
distinction. Compare:

case 1,2,3: # Tuple - checks for any of its members
case range(10,110,10): # Range object - beautiful, convenient!
case 42: # Non-iterable, works the way you'd expect
case {1:"Test",2:"Hello",3:[]}: # Dictionary, may be surprising
case "Test",: # Tuple with a single string in it - works but ugly
case "Test": # And there's your problem.

You now have three valid ways to interpret that last statement:
1) It matches the exact string "Test", the way people will expect
2) It matches any of the results of iterating over it
3) It matches anything where x in "Test" is true

The first option is a big fat special case, and one that'll get more
complicated as you start looking at subclasses and such. The second
means that "case x" is equivalent to "case tuple(x)", but I can't
imagine people would actually want that in real usage. The third is
the way you've described it so far, but again, I cannot imagine it as
anything other than highly surprising that my last statement above
will match "st" and "es".

It would be more invasive to the language, but possibly better, to
have a new magic method __case__ which gets called to see if this
object matches this switched object. If that isn't defined, an
equality check is done. Then all of the above cases can be made
unsurprising by simply defining __case__ on a tuple (membership test)
and a range (ditto, except that the members aren't specifically
instantiated); everything else will check equality. (Having a dict
check for its keys is surprising, IMO, and I don't mind that one not
working.) It'd make it a *lot* easier to ensure sane behaviour in
custom classes; if you want your class to function like a single unit,
don't define __case__, but if you want it to function like a
collection, set __case__=__in__. What do you reckon?

ChrisA

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


Thread

Re: Yet Another Switch-Case Syntax Proposal Chris Angelico <rosuav@gmail.com> - 2014-04-03 08:50 +1100
  Re: Yet Another Switch-Case Syntax Proposal Steven D'Aprano <steve@pearwood.info> - 2014-04-03 01:06 +0000
    Re: Yet Another Switch-Case Syntax Proposal Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-03 00:50 -0600

csiph-web