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

Sean 'Shaleh' Perry shalehperry at attbi.com
Wed May 29 18:36:52 EDT 2002


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

depending on implementation is almost never a good idea.  Sure CPython behaves
this way now, but it may not next month or next year.  More importantly this
style of coding makes the creation of temporaries common and blessed where one
usually does not want an abundance of them.

The arguments then move to pure aesthetic style and we all know that no one can
agree there so there is no need to carry it further.

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

you can choose to not close() a file in python (either implementation) you just
have to hope the interpreter gets to it soon.  In one this happens to be true,
in the other it may not be.  As a matter of habit in any long running program I
am always close() what needs to be closed and in other way attempt to help the
garbage collector.





More information about the Python-list mailing list