On Wed, Aug 10, 2011 at 19:15, Branko Čibej <br...@xbc.nu> wrote:
>...
> Explicit management of rare resources is no bad thing. The "with"
> statement was introduced specifically to do away with the flaky, three
> times longer try-except-else that would otherwise be necessary in order
> to guarantee that file objects are closed.

With the prior constructions:

open(foo).read()
open(foo, 'w').write(bar)

If one of those .read() or .write() calls fails, *and* somebody does
not clear the traceback... then yeah. A file remains open, referenced
from the traceback. But somebody has to be explicitly *keeping* those
tracebacks. I've never seen code where people keep lists of
tracebacks. I cannot even speculate on why somebody might use svn.fs,
capture tracebacks, hold onto them, and then *continue* trying to use
svn.fs.

Switching to:

with open(foo) as f:
  f.read()

You're going from a single expression to a control flow construct,
simply to try and avoid a situation that I doubt anybody could even
begin to argue would come up.

> And yes, the CPython docs do say that file objects are closed when their
> refcount reaches zero, but also state that one shouldn't rely on that.
> Also see the second stanza in PEP 20. :)

Ha! And the third stanza says "Simple is better than complex."

> There's no call to jump all over people because you don't agree with
> their coding style.

If you call that "jump[ing] all over people", then I think you're
taking it wrong. I'm providing review feedback where I see a simple
construction being turned into something more complicated.

Cheers,
-g

Reply via email to