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


Groups > comp.lang.python > #12608

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

From Dan Sommers <dan@tombstonezero.net>
Subject Re: idiomatic analogue of Perl's: while (<>) { ... }
Date 2011-09-02 02:32 +0000
References <mailman.644.1314853527.27778.python-list@python.org> <4e5f2010$0$29987$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.681.1314930753.27778.python-list@python.org> (permalink)

Show all headers | 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