[Python-Dev] PEP 340 - possible new name for block-statement

Nick Coghlan ncoghlan at gmail.com
Fri Apr 29 17:26:13 CEST 2005

Jim Jewett wrote:
> Why not just agressively run the finalization on both forms when the
> reference count permits?

So the iterator is always finalised if the for loop has the only reference?

Two problems I can see there is that naming the target of the for loop would 
prevent it being finalised, and that this would make life interesting when the 
Jython or IronPython folks catch up to Python 2.5. . .

The finalised/not finalised aspect definitely seems to be the key behavioural 
distinction between the two forms, though. And I think there are legitimate use 
cases for a non-finalised form. Things like:

    for line in f:
        if end_of_header(line):
        # process header line

    for line in f:
        # process body line

With only a finalised form of iteration available, this would need to be 
rewritten as something like:

     def header(f):
         line = next(f)
         while not end_of_header(line):
             line = next(f, yield line)

    for line in header(f):
        # process header line
    for line in f:
        # process body line

Considering the above, I actually have grave reservations about *ever* making 
finalisation the default behaviour of for loops - if I break out of a standard 
for loop before exhausting the iterator, I would expect to be able to resume the 
iterator afterwards, rather than having it flushed behind my back.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-Dev mailing list