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

Donn Cave donn at drizzle.com
Thu May 30 02:09:53 EDT 2002


Quoth "Delaney, Timothy" <tdelaney at avaya.com>:
| > 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.

The real Python _is_ implementation.  We all know that you actually
can rely on timely finalization, and we can guess why the documentation
says what it says.

| 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?

The same applies to memory.  If objects were finalized when they still
have references, or if their memory were released, that would be an
error, and I would not be in favor of that.

| 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.

Same as (2) - it's my responsibility to design my program so that it
is "correct" with respect to object lifetimes, meaning that objects
don't live longer than I want them to.  It's axiomatic in Python that
an object with references is still valid - not just a matter of
documented or implemented behavior - and it follows that if I want
an object's lifetime to expire, I have to make sure there are no
references to it.  I'm cool with that.  Most of the time I will not
care anyway, but to the extent it matters, this is a much better
deal than having to reinvent reference counting while I pretend it
isn't already being done by the interpreter.

	Donn Cave, donn at drizzle.com



More information about the Python-list mailing list