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


Groups > comp.lang.python > #63023

Re: Ifs and assignments

Path csiph.com!usenet.pasdenom.info!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; '(at': 0.04; 'obj': 0.09; 'cc:addr:python-list': 0.11; 'jan': 0.12; '(var': 0.16; 'false:': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterating': 0.16; 'likely.': 0.16; 'statements,': 0.16; 'true:': 0.16; 'language': 0.16; 'wrote:': 0.18; 'solution.': 0.20; 'cc:addr:python.org': 0.22; 'this?': 0.23; "shouldn't": 0.24; 'header': 0.24; 'looks': 0.24; 'cc:2**0': 0.24; 'second': 0.26; 'gets': 0.27; 'header:In-Reply- To:1': 0.27; 'point': 0.28; 'function': 0.29; 'message- id:@mail.gmail.com': 0.30; "d'aprano": 0.31; 'indentation': 0.31; 'steven': 0.31; 'probably': 0.32; 'fri,': 0.33; 'maybe': 0.34; "i'd": 0.34; 'common': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'really': 0.36; 'var': 0.36; 'should': 0.36; 'example,': 0.37; 'turn': 0.37; 'level': 0.37; 'pm,': 0.38; 'that,': 0.38; 'how': 0.40; 'most': 0.60; 'break': 0.61; 'simple': 0.61; 'places': 0.64; 'more': 0.64; 'biggest': 0.67; 'frequently': 0.68; 'condition.': 0.84; '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=yiokYzyIK06r2MMO8uBBysPAQUUNp77tU7z+Pp55RpQ=; b=Az6XaIr9Sl9EIksyB7xj2lu5MXLw76lJTzxR0j7xLJJ45C5fyOvUj5jspNiKi5Xi3l UXtLTXReaLtUyHe0dx9Cep6BUPP9b4mrHebp814p3DGaSsYBAu/QdfubaokXnybwnOtS wPIlHETqelI6ul8DZoemguIvntCmpGHeVLdhNzWcGft1uTvU7wBUFDNUMP4GMNxbAAwN OL5rULHZQaKgba/jd8vhcTVzpmK4NJo/C9YbYRx+2gkUltXmHuwtdQHvJQjApsDSpoaV qovZZ5IgnpvZQi7bVlzZo20OP7MOeI31l1BSo3eF72mFoo+eHE/Pa/rgOtp63Wl1utv4 Zlqg==
MIME-Version 1.0
X-Received by 10.66.66.234 with SMTP id i10mr49618195pat.127.1388720981626; Thu, 02 Jan 2014 19:49:41 -0800 (PST)
In-Reply-To <52c62fa8$0$30001$c3e8da3$5496439d@news.astraweb.com>
References <52C59FF6.5000607@allsup.co> <mailman.4816.1388709915.18130.python-list@python.org> <52c62fa8$0$30001$c3e8da3$5496439d@news.astraweb.com>
Date Fri, 3 Jan 2014 14:49:41 +1100
Subject Re: Ifs and assignments
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.4823.1388720985.18130.python-list@python.org> (permalink)
Lines 46
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1388720985 news.xs4all.nl 2841 [2001:888:2000:d::a6]:42791
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:63023

Show key headers only | View raw


On Fri, Jan 3, 2014 at 2:33 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> However, I don't think we should treat this as specific to if statements:
>
>     for i, obj in enumerate(alist + blist + clist as items):
>         items[i] = process(obj)
>
>
> Is that really better than this?
>
>     items = alist + blist + clist
>     for i, obj in enumerate(items):
>         items[i] = process(obj)

It definitely shouldn't be specific to if statements, though I think
that's probably going to be the biggest use-case (chained if/elif
blocks). Maybe a for loop isn't the best other example, but I
frequently work with places where I want to call some function and
keep iterating with the result of that until it returns false:

while (var = func())
{
    ....
}

In Python, that gets a lot clunkier. The most popular way is to turn
it into an infinite loop:

while True:
    var = func()
    if not var: break
    ....

which is pretty much how the C version will compile down, most likely.
It feels like an assembly language solution. The point of a while loop
is to put its condition into the loop header - burying it inside the
indented block (at the same indentation level as most of the code)
conceals that, and this is a very simple and common condition. So this
is what I'd cite as a second use-case:

while func() as var:
    ....

And that definitely looks more readable.

ChrisA

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


Thread

Re: Ifs and assignments Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-01-02 19:45 -0500
  Re: Ifs and assignments Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-01-03 14:33 +1100
    Re: Ifs and assignments Chris Angelico <rosuav@gmail.com> - 2014-01-03 14:49 +1100
      Re: Ifs and assignments Roy Smith <roy@panix.com> - 2014-01-02 23:14 -0500
        Re: Ifs and assignments Chris Angelico <rosuav@gmail.com> - 2014-01-03 15:25 +1100
        Re: Ifs and assignments Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-03 04:29 +0000
      Re: Ifs and assignments Duncan Booth <duncan.booth@invalid.invalid> - 2014-01-03 15:46 +0000
    Re: Ifs and assignments Chris Angelico <rosuav@gmail.com> - 2014-01-03 14:55 +1100
      Re: Ifs and assignments Roy Smith <roy@panix.com> - 2014-01-02 23:00 -0500
        Re: Ifs and assignments Chris Angelico <rosuav@gmail.com> - 2014-01-03 15:08 +1100

csiph-web