A "scopeguard" for Python

Robert Kern robert.kern at gmail.com
Thu Mar 4 12:00:32 EST 2010


On 2010-03-04 10:32 AM, Jean-Michel Pichavant wrote:
> Alf P. Steinbach wrote:
>> * Jean-Michel Pichavant:
>>> Alf P. Steinbach wrote:
>>>>> From your post, the scope guard technique is used "to ensure some
>>>>> desired cleanup at the end of a scope, even when the scope is
>>>>> exited via an exception." This is precisely what the try: finally:
>>>>> syntax is for.
>>>>
>>>> You'd have to nest it. That's ugly. And more importantly, now two
>>>> people in this thread (namely you and Mike) have demonstrated that
>>>> they do not grok the try functionality and manage to write incorrect
>>>> code, even arguing that it's correct when informed that it's not, so
>>>> it's a pretty fragile construct, like goto.
>>>
>>> You want to execute some cleanup when things go wrong, use try
>>> except. You want to do it when things go right, use try else. You
>>> want to cleanup no matter what happen, use try finally.
>>>
>>> There is no need of any Cleanup class, except for some technical
>>> alternative concern.
>>
>> Have you considered that your argument applies to the "with" construct?
>>
>> You have probably not realized that.
>>
>> But let me force it on you: when would you use "with"?
>>
>> Check if that case is covered by your argument above.
>>
>> Now that you've been told about the "with" angle, don't you think it's
>> a kind of weakness in your argument that it calls for removing "with"
>> from the language?
>>
>> I recommend that you think about why your argument is invalid.
>>
>> Or, as I like to say, why your argument is completely bogus.
>>
>>
>> Cheers & hth.,
>>
>> - Alf
> 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




More information about the Python-list mailing list