EOF

David Bolen db3l at fitlinxx.com
Fri Jul 7 20:36:51 EDT 2000


"Daley, MarkX" <markx.daley at intel.com> writes:

> A better way is this:
> 
> f = open("file")
> line = f.readline
> while line:
> 	<process here>
> 	line = f.readline

Well, better is debatable I suppose :-) I personally dislike code like
this a lot since it places the executing code on which the loop
depends on two places.  For a single readline() (don't forget the ())
maybe it isn't terrible, but it's still a maintenance headache waiting
to happen - you'll always have two spots that need to be kept exactly
the same.

> This way, you're not testing truth against a known true.

The pattern of an infinite loop being broken upon an exit condition is
a fairly standard approach for a number of situations, and I think
it's pretty clear.  At least for the case of file I/O in Python, the
while 1: with a break on EOF loop is pretty much an idiom - argued at
times as one of the rougher idioms, but an idiom nonetheless :-)

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list