[Python-ideas] "try with" syntactic sugar

Alec Thomas alec at swapoff.org
Thu Feb 26 16:14:44 CET 2009


2009/2/27 Eric Smith <eric at trueblade.com>:
> Daniel Stutzbach wrote:
> ...
>>
>> The "with" statement is great, but it results in the bulk of the code
>> being indented twice.  I'd like to propose a little syntactic sugar, the
>> "try with":
>>
>> try with something as f:
>>    many lines of code
>> except some_error:
>>    handle error
>
> Not commenting on how useful the proposal is (I'm mostly stuck in Python
> 2.4), but couldn't this be:
>
> with something as f:
>    many lines of code
> except some_error:
>    handle error

+1

Like the OP, I find myself doing this same sequence of code over and
over. The syntax you describe here would clean the code up
considerably.
-- 
"Life? Don't talk to me about life." - Marvin



More information about the Python-ideas mailing list