R
Robert Kern
I am using python 2.5, so I know nothing about the with statement,
You can try it out using "from __future__ import with_statement".
and
it may possible my arguments apply to it, you could remove it from the
language, it wouldn't bother me at all.
I just don't see in what you've written (adding a class, with some
__entry__, __exit__ protocol, using a with statement) what cannot be
achieved with a try statement in its simpliest form.
Try except may be lame and noobish, but it works, is easy to read and
understood at first glance.
It looks like to me that 'with' statements are like decorators:
overrated. Sometimes people could write simple readable code, but yet
they're tempted by the geek side of programming: using complex
constructs when there's no need to. I myself cannot resist sometimes ;-)
PEP 343 is a good introduction to the real uses of the with: statement.
http://www.python.org/dev/peps/pep-0343/
Basically, it allows you to package up your initialization and cleanup code into
objects, stick them in your library, unit test them thoroughly, etc. so you
don't have to repeat them everywhere and possibly get them wrong. It's DRY in
action.
Where Alf's Cleanup class goes wrong, in my opinion, is that it does not package
up any code to avoid repetition. You still repeat the same cleanup code
everywhere you use it, so it is no better than try: finally:. It is not a real
use case of the with: statement.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco