A "scopeguard" for Python

Jean-Michel Pichavant jeanmichel at sequans.com
Thu Mar 4 11:50:06 CET 2010


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.

JM





More information about the Python-list mailing list