"Try:" which only encompasses head of compound statement

Jameson.Quinn at gmail.com Jameson.Quinn at gmail.com
Tue Aug 28 19:51:16 EDT 2007


Those are some good responses, but I think they focused a little too
much on the specifics of my example - especially the 'print'
statement. In real life, I want to set some return values to sensible
defaults if the file doesn't exist, but I want the errors from inside
the block to trickle up as normal. And yes, I can easily use a
temporary variable for the open file, but it really seems to me that
this defeats the purpose of the 'with' statement - if this were
something more dangerous than a file, I'd feel mighty leery of having
it in a temporary variable outside a 'with' for even a moment.

So, the options are:
-temporary variable - could this EVER be more dangerous than the with
statement (KeyboardInterrupt which is later caught and handled?)
-'tunnel' internal exceptions past the try by wrapping them in a
TunnelException and then unwrapping them with a containing try block.

The second looks like a hack to me, and the first still feels
dangerous. So I still think that some syntax here would be a bit more
than just sugar.




More information about the Python-list mailing list