Paul Rubin wrote:
> Dan <[EMAIL PROTECTED]> writes:
> > Is this discouraged?:
> >
> >      for line in open(filename):
> >          <do something with line>
>
> Yes.
>
> > Can I count on the ref count going to zero to close the file?
>
> You really shouldn't.  It's a CPython artifact.

I disagree, somewhat.  No, you shouldn't count on the "ref count" per
se going to 0.  And you shouldn't count on the file object being GC'd
_immediately_ after the last reference is destroyed. You should be able
to rely on it being GC'd at some point in the not-horribly-distant
future, though.

Doing an explicit .close() is not normally useful and muddies the code
(and introduces more lines for potential bugs to infest).

And yes, I know that the language spec technically allows for no GC at
all--it's a QOI issue, not a spec issue, but any implementation that
didn't GC would be useless as a general Python platform (perhaps useful
for specific embedded uses, but programming for such an environment
would be different from programming for rational python platforms in
bigger ways than this).

(And personally I think the benefits to programmers of guaranteeing
ref-counting semantics would outweigh the additional headaches for
Jython and other alternative implementations).

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

Reply via email to