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

François Pinard pinard at iro.umontreal.ca
Wed May 29 18:30:58 EDT 2002


[Gary Herron]

> Thus the "bad" part of this programming practice is that the timing
> of the implied close is dependent Python implementation issues (i.e.,
> the timing of the garbage collection), and such dependencies are never
> a good thing to rely on.

There might be cases when one moves between Python and Jython, indeed.
When one knows s/he works with Python only, it is good style to rely on
the refcount behaviour, as it yields code which is not only more legible,
but also more elegant and concise.  It means that you understand and
accept in advance having to revise your code if you ever want to use other
implementations of Python, like Jython.  As someone was pointing to me
very recently, the Python reference tries to describe a common language,
but there is no "C-Python" specific guide.  If there was one, the refcount
behaviour would most probably be described as dependable and reliable,
even through future versions, as far as Python programming is concerned.

And besides, it seems that the few implementations of Python do not support
exactly the same language: extensions here may not be available there.
Defining "good style" as the common subset of all Python implementations,
and everything else as "bad style", seems questionable.  The only thing
is that you have to be aware of the implications of your choices.

For this "Jython-forces-you-to-explicit-closes" matter, my feeling is that
Jython encourages bad style here, much more than it defines good style.
Surely, there has never been a bad intent from Jython author.  We understand
that the limitation comes from the fact Jython relies on the Java system
for collecting garbage.  One has to close explicitly in Jython for practical
implementation considerations, this has nothing to do with good style.

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard






More information about the Python-list mailing list