[Python-Dev] Acquire/release functionality
Aahz
aahz@pythoncraft.com
Mon, 3 Feb 2003 10:13:35 -0500
On Mon, Feb 03, 2003, Moore, Paul wrote:
>
> In trying to draw a parallel between "with" and the C++ RAII idiom, I
> hit a funny, best illustrated by an example:
>
> class autoclose(file):
> __exit__ = close
>
> with f = autoclose("blah.txt", "r"):
> # Let's get sneaky
> g = f
> # Main code here
>
> # File now gets closed
> with g:
> # Who's a naughty boy then - g is a closed file
> # And the g.__exit__() call goes boom here...
>
> OK, it's contrived. The equivalent C++ risk is that of creating a dangling
> pointer to a local variable which is deleted, which is also rare.
It's not at all contrived. And I think it's perfectly acceptable; it's
no different, fundamentally, from calling f.close() twice by mistake.
--
Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/
"Argue for your limitations, and sure enough they're yours." --Richard Bach