Fri Jul 25 22:35:49 CEST 2003
bokr at oz.net (Bengt Richter) writes:
> >"done" here is a generic method that gets called on exiting a "with"
> First reaction == +1, but some questions...
> 1) Is f.done() really necessary? I.e., doesn't an explicit del f
> take care of it if the object has been coded with a __del__
> method? I.e., the idea was to get the effect of CPython's
> immediate effective del on ref count going to zero, right?
The ref count might not be zero. Something inside the "with" block
might make a new reference and leave it around.
> 2) how about with two files (or other multiple resources)?
> with f1,f2 = file('f1'), file('f2'):
> [do stuff with f1 & f2]
> What is the canonical expansion?
I think f1.done and f2.done should both get called.
> Also, what about the attribute version, i.e.,
> ob.f = file(frob)
> [do stuff with ob.f]
> del ob.f # calls f.__del__, which calls/does f.close()
> I.e., ob.f = something could raise an exception (e.g., a read-only property)
> *after* file(frob) has succeeded. So I guess the easiest would be to limit
> the left hand side to plain names...
The assignment should be inside the try. If I had it on the outside
before, that was an error.
More information about the Python-list