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


Groups > comp.lang.python > #12608

Re: idiomatic analogue of Perl's: while (<>) { ... }

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; '(although': 0.05; 'infinite': 0.07; 'raises': 0.07; 'subject:while': 0.07; 'none:': 0.09; 'parsing': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; '"while': 0.16; 'input,': 0.16; 'wrote:': 0.16; 'otherwise,': 0.19; 'slightly': 0.19; 'seems': 0.20; 'sep': 0.23; 'input': 0.24; 'string': 0.26; 'loop': 0.28; 'thu,': 0.28; 'fine.': 0.29; 'looks': 0.29; 'print': 0.29; 'controls': 0.30; 'equivalent': 0.31; 'received:24': 0.32; 'actual': 0.32; 'to:addr:python-list': 0.33; 'that,': 0.33; "i've": 0.34; 'header:User-Agent:1': 0.34; 'describe': 0.34; 'header:X-Complaints-To:1': 0.35; 'unless': 0.36; 'but': 0.37; 'received:org': 0.38; 'steven': 0.38; 'subject:: ': 0.39; 'header :Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'more': 0.60; '"there': 0.84; 'idiomatic': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dan Sommers <dan@tombstonezero.net>
Subject Re: idiomatic analogue of Perl's: while (<>) { ... }
Date Fri, 2 Sep 2011 02:32:17 +0000 (UTC)
References <mailman.644.1314853527.27778.python-list@python.org> <4e5f2010$0$29987$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host 24.100.92.194
User-Agent Pan/0.133 (House of Butterflies)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.681.1314930753.27778.python-list@python.org> (permalink)
Lines 32
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1314930753 news.xs4all.nl 2542 [2001:888:2000:d::a6]:44465
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12608

Show key headers only | View raw


On Thu, 01 Sep 2011 16:02:54 +1000, Steven D'Aprano wrote:

> On Thu, 1 Sep 2011 02:56 pm Sahil Tandon wrote:

>> # process input, line-by-line, and print responses after parsing input
>> while 1:
>>   rval = parse(raw_input())
>>   if rval == None:
>>     print('foo')
>>   else:
>>     print('bar')
>> %%

> "while True" is considered slightly more idiomatic (readable), but
> otherwise, that seems fine.

Arguably more readable, but arguably less idomatic, is to describe the
actual condition that controls the loop in a string (non-empty strings
are equivalent to True in this context):

while "there is more input":
    rval = parse(raw_input())
    if real is None:
        print('foo')
    else:
        print('bar')

(Although now that I've said that, this looks like an infinite loop
unless parse, raw_input, or print raises an exception.)

Dan

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


Thread

idiomatic analogue of Perl's: while (<>) { ... } Sahil Tandon <sahil@FreeBSD.org> - 2011-09-01 00:56 -0400
  Re: idiomatic analogue of Perl's: while (<>) { ... } Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-01 16:02 +1000
    Re: idiomatic analogue of Perl's: while (<>) { ... } Sahil Tandon <sahil@FreeBSD.org> - 2011-09-01 22:02 -0400
    Re: idiomatic analogue of Perl's: while (<>) { ... } Dan Sommers <dan@tombstonezero.net> - 2011-09-02 02:32 +0000
  Re: idiomatic analogue of Perl's: while (<>) { ... } Anssi Saari <as@sci.fi> - 2011-09-01 18:25 +0300

csiph-web