A "scopeguard" for Python

Mike Kent mrmakent at gmail.com
Thu Mar 4 22:12:01 CET 2010

On Mar 4, 12:30 pm, Robert Kern <robert.k... at gmail.com> wrote:

> He's ignorant of the use cases of the with: statement, true.

<humor> Ouch!  Ignorant of the use cases of the with statement, am I?
Odd, I use it all the time. </humor>

> Given only your
> example of the with: statement, it is hard to fault him for thinking that try:
> finally: wouldn't suffice.

<humor> Damn me with faint praise, will you? </humor>

I'm kinda amazed at the drama my innocent request for the use case
elicited.  From what I've gotten so far from this thread, for the
actual example Mr. Steinbach used, the only disadvantage to my counter-
example using try/finally is that the chdir in the finally part will
always be executed, even if the chdir in the try part did not
succeed.  I concede that, and was aware of it when I wrote it.  For
the simple example given, I did not consider it compelling.  A more
complex example, that would have required multiple, nested try/finally
blocks, would show the advantages of Mr Steinbach's recipe more

However, I fail to understand his response that I must have meant try/
else instead, as this, as Mr. Kern pointed out, is invalid syntax.
Perhaps Mr. Steinbach would like to give an example?

More information about the Python-list mailing list