[Python-Dev] PEP 340: Deterministic Finalisation (new PEP draft, either a competitor or update to PEP 340)

Jim Jewett jimjjewett at gmail.com
Mon May 9 17:07:36 CEST 2005


Nick Coghlan wrote:
> "Loop on this iterator and finalise when done" would be written:

>    for item in itr:
>        process(item)
>    finally:
>        pass

Greg Ewing wrote:

> This is approaching things from the wrong end. The user of
> an iterator shouldn't need to know or care whether it
> requires finalization -- it should Just Work, whatever
> context it is used in.

If you're saying "lists don't need it, but openfiles do", then I agree;
it shouldn't matter what type of iterator you have.

If you're talking specific object instances, then the user is the 
only one (outside of the Garbage Collection system) who has a 
chance of knowing whether the rest of the iterator will be needed
later.

When iterating over lines in a file, and breaking out at a sentinel,
the compiler can't know whether you're done, or just leaving the
"real" lines to another piece of code.

Of course, that still raises the "Why are we encouraging bugs?" issue.

If there are no remaining references, then garbage collection is the
answer, and maybe we just need to make it more aggressive.  If
there are remaining references, then maybe the user is wrong about
being done.

-jJ


More information about the Python-Dev mailing list