kj wrote:
There's something wonderfully clear about code like this:

    # (1)
    def spam(filename):
        for line in file(filename):
            do_something_with(line)

It is indeed pseudo-codely beautiful.  But I gather that it is not
correct to do this, and that instead one should do something like

    # (2)
    def spam(filename):
        fh = file(filename)
        try:
            for line in fh:
                do_something_with(line)
        finally:
            fh.close()

...or alternatively, if the with-statement is available:

    # (3)
    def spam(filename):
        with file(filename) as fh:
            for line in fh:
                do_something_with(line)

Mind you, (3) is almost as simple as (1) (only one additional line),
but somehow it lacks (1)'s direct simplicity.  (And it adds one
more indentation level, which I find annoying.)  Furthermore, I
don't recall ever coming across either (2) or (3) "in the wild",
even after reading a lot of high-quality Python code (e.g. standard
library modules).

Finally, I was under the impression that Python closed filehandles
automatically when they were garbage-collected.  (In fact (3)
suggests as much, since it does not include an implicit call to
fh.close.) If so, the difference between (1) and (3) does not seem
very big.  What am I missing here?

kynn

We have to distinguish between reference counted and garbage collected. As MRAB says, when the reference count goes to zero, the file is immediately closed, in CPython implementation. So all three are equivalent on that platform.

But if you're not sure the code will run on CPython, then you have to have something that explicitly catches the out-of-scopeness of the file object. Both your (2) and (3) do that, with different syntaxes.

DaveA

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to