idiomatic analogue of Perl's: while (<>) { ... }
Dan Sommers
dan at tombstonezero.net
Thu Sep 1 22:32:17 EDT 2011
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
More information about the Python-list
mailing list