two ideoms at one blow: line-reading and regexp-matching

Russell E. Owen owen at astroNOJNK.washington.edu.invalid
Wed Feb 20 12:39:29 EST 2002


In article <m2vgcs5hgf.fsf at desk.crynwr.com>, Russell Nelson 
<nelson at crynwr.com> wrote:

>There are two Python ideoms that bother me:
>
>    while 1:
>        line = file.readline()
>        if not line: break

I agree this is ugly. Fortunately, the language designers did too and 
offer a better way in Python 2.2. If you are willing to only read the 
file from a single for loop, you can write:

for line in file:
   ...process the line

However, if you want to read part of the file now and the rest later in 
your program, do not use this syntax. The first for loop usually reads 
more of the file than it gives you back (for the sake of efficiency), so 
you'll miss lines if you then read more later. In that case, use 
iterators...

There was a recent discussion of this, but I was never clear if there 
was a best way to get an efficient file iterator. There is no 
file.iter() call, which is a great pity (since it would be unambiguous). 
In lieu of that, the following is probably reasonable, but use at your 
own risk:

fileiter = file.xreadlines()
for line in fileiter:
  # process some of the file

# then later...
for line in fileiter:


Regarding assignments returning values...it is discussed every once in 
awhile and I don't think it'll change. (For the record, I find 
assignments returning values somewhat confusing and error-prone. But I 
realize others do like them and admit that they can shorten code.)

-- Russell



More information about the Python-list mailing list