[Python-Dev] Acquire/release functionality
holger krekel
pyth@devel.trillke.net
Mon, 3 Feb 2003 18:03:35 +0100
Moore, Paul wrote:
> Paul Moore <lists@morpheus.demon.co.uk> writes:
>
> > As far as the acquire/release case is concerned, I think that there is
> > definitely a strong case for syntactic support for this. The
> > equivalent idiom in C++ is "resource acquisition is initialisation"
> > which, while not having syntax support, does rely on the existence of
> > deterministic destructors, and on the ability to introduce a new scope
> > at will. Python has neither of these, and the try...finally construct
> > is verbose enough to make something better worth having.
>
> 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:
I haven't found a way to make assignments optional (in recent
experiments). Is this possible with Python's Parser?
holger