Why no open(f, "w").write()?

Delaney, Timothy tdelaney at avaya.com
Thu May 30 00:58:03 EDT 2002


> From: Donn Cave [mailto:donn at drizzle.com]
> 
> You're describing a language other than C Python.  In C Python, you
> *may* close files and rely on other kinds of finalizations through
> the reference counting garbage collector, and the result is as timely
> as if you did it explicitly.  That's finalization as it ought to be,
> and it's a shame that other languages compromise on that.  Lots of
> people don't think it's important, but then the majority of 
> programmers
> think it's OK that their programming language requires them to keep
> track of memory in the same way.

I would be very happy for this to be the case.

However, CPython is not a language where you can rely on timely
finalisation.

1. The documentation explicitly states that you cannot. Anything else is an
implementation artifact.

2. The thing you expect to be finalised may have another reference
somewhere. Are you 100% sure that no function you've passed it to has held a
reference to it in a cache?

3. Your object may be in a cycle. This will result in a non-timely
finalisation.

4. Your object may be in a cycle where Python explicitly punts and doesn't
run the finalisers.

Tim Delaney





More information about the Python-list mailing list