[Python-Dev] With statement
Oren Tirosh
oren-py-d@hishome.net
Wed, 5 Feb 2003 12:12:29 -0500
On Wed, Feb 05, 2003 at 01:35:57PM +0000, Michael Hudson wrote:
> Oren Tirosh <oren-py-d@hishome.net> writes:
>
> > Let's assume that a new 'local' statement is added to Python. At the end
> > of the current block the __del__ method of any references declared local
> > is guaranteed to be called. The object must have exactly one remaining
> > reference, otherwise an exception is raised. In a GC implementation it may
> > make some change to the object so that any remaining references to it
> > become stale references and raise an exception as soon as they are touched
> > in any way. Note that a reference to a closed file object is already a
> > stale reference and raises an exception if any I/O operation is attempted
> > on it.
>
> Two comments:
>
> 1) The resource-allocation-is-initialization pattern works very well
> in C++, but I'm unconvinced that we should aim to copy it exactly
I am not copying anything. I came up with this idea all by myself ;-)
> -- it's a nice solution to the problem, but not the only possible
> or best one, IMHO.
Umm... after reading this sentence a few times, I am still not sure what
information it conveys to me about your opinion of the idea.
Was that a +1, +0, -0 or -1?
> 2) Without the ability to define a scope at will, RAiI becomes less useful.
local f=open('sdaskdfjhaslk')
...
del l
Deleting this kind of variable (whether we call it local or something
else) means you want its __del__ executed now. It is also an assertion
that its reference count is supposed to be 1 at this point. This not only
gives you the ability to define a scope at will, it lets you create
scopes dynamically. It's probably useful for more than just RAiI. Perhaps
too useful.
Oren