[Python-ideas] "try with" syntactic sugar

Cesare Di Mauro cesare.dimauro at a-tono.com
Thu Feb 26 16:19:13 CET 2009


On Feb 26, 2009 at 16:14 PM, Alec Thomas <alec at swapoff.org> wrote:

> 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.

+1 Same for me. It avoids an additional try: except: surrounding the with.



More information about the Python-ideas mailing list