On Wed, Aug 10, 2011 at 17:44, Hyrum K Wright <hyrum.wright_at_wandisco.com> wrote:
>> And now where did your .close() call go? You're still relying on 'f'
>> going out of scope to close. IOW, right back to the zero refcount
>> algorithm. And in which case, the original construction is much
>> cleaner than all this "with" gunk.
> In the case of an open()'d file, the file is closed when the object is
> eventually garbage collected, which, by documentation isn't
> guaranteed to happen when the object is unreachable: "Do not depend on
> immediate finalization of objects when they become unreachable (ex:
> always close files)." Hence the need for an explicit close.
That is referring to circular references, where all objects in the
circle are (no longer) referenced from outside the circle.
You get a reference to the file, it is stashed into a bound method
object, the method is invoked with another reference increasing as the
file is bound to 'self'. When the method returns, the bound method is
decref'd which decref's the file, which closes it.
> As for "gunk," the with construction was introduced in Python 2.5,
> almost 5 years ago and I find it cleaner.
I find the use of control flow construct to replace a simple
expression... to be more complicated. Has no bearing on how long
'with' has been around.
And now that you mention it... we have always said that our delivered
Python code (as distinct from our build/test environment) should be
compatible with Python 2.4. I don't recall that we've changed that
Received on 2011-08-11 01:42:36 CEST