Warning about "for line in file:"

Alex Martelli aleax at aleax.it
Mon Feb 18 04:51:23 EST 2002


Neil Schemenauer wrote:

> Aldo Cortesi wrote:
>> Actually this has nothing to do with iterators, or a "read
>> cache".
> 
> It does.
> 
>> iter(file) creates a line iterator that does the
>> same thing as file.readline() every time .next() is called,
>> until it reaches the end of the file.
> 
> It does not.  iter(file) calls file.xreadlines().  xreadlines()
> internally calls readlines(CHUNKSIZE).

Right.  If you want an iterator that works exactly as Aldo
just described, albeit at substantial performance cost,
iter(file.readline, '') is the handiest way to build one.

The general form for two-argument iter is
    iter(callable, sentinel)

and it works just about like:

def iter2(callable, sentinel):
    while 1:
        result = callable()
        if result==sentinel: return
        yield result

so that:

for item in iter(callable, sentinel):
    process(item)

is a handy way of expressing:

while 1:
    item = callable()
    if item==sentinel: break
    process(item)



Alex




More information about the Python-list mailing list