Greg Ewing wrote:
> Ron Adam wrote:
> 
>>There seems to be some confusion as to weather or 
>>not 'for's will do finalizing.  So I was trying to stress I think 
>>regular 'for' loops should not finalize. They should probably give an 
>>error if an object with an try-finally in them or an __exit__ method. 
> 
> 
> But if the for-loop can tell whether the iterator
> needs finalizing or not, why not have it finalize
> the ones that need it and not finalize the ones
> that don't? That would be backwards compatible,
> since old for-loops working on old iterators would
> work as before.

When I first started redrafting the PEP, I had essentially this idea in there - 
look for an __exit__() method, if it's there use the new 'finalising' 
semantics, 
if it isn't, use the old semantics.

This bloats the generated byte code horribly, though - it is necessary to 
produce two complete copies of the for loop code, since we don't know at 
compile 
time which version (finalising or non-finalising) will be needed.

It also takes away from the programmer the ability to choose to do partial 
iteration on generators that require finalisation. And it does so in a 
non-obvious way: "it works with this iterator, why doesn't it work with that 
one?"

Accordingly, I switched to a version which puts control pack in the hands of 
the 
programmer. If you know the iterator doesn't need finalising, or if eventual 
finalisation on garbage collection is sufficient, then you can omit the finally 
clause, and get the optimised form of the for loop. Alternatively, if you want 
prompt finalisation (e.g. with an iterator like all_lines() from the PEP 
redraft), then you can include the finally clause and get the behaviour you 
want.

Failure to finalise promptly on iterators that need it is still a bug - but 
then, so is failing to close a file handle or database connection.

Cheers,
Nick.

-- 
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.blogspot.com
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to