[Python-ideas] Possible PEP regarding the use of the continue keyword in try/except blocks

Eric V. Smith eric at trueblade.com
Mon Jan 7 10:30:59 EST 2019


On 1/7/2019 10:15 AM, Chris Angelico wrote:
> On Mon, Jan 7, 2019 at 11:11 PM Oscar Benjamin
> <oscar.j.benjamin at gmail.com> wrote:
>> On Mon, 7 Jan 2019 at 09:27, Chris Angelico <rosuav at gmail.com> wrote:
>>> On Mon, Jan 7, 2019 at 7:11 PM Anders Hovmöller <boxed at killingar.net> wrote:
>>>>
>>>>> This proposal is basically about introducing goto to the language.
>>>> A bit hyperbolic but I agree that it has the same problem as goto. But the specific suggested solution is not something we should be restricted so rigidly to in this discussion. One could for example see another solution to the same problem:
>>>>
>>>> with supress_raise(TypeError, ValueError):
>>>>     do_the_things()
>>>>
>>>> I have no idea how to actually implement this though and it's also a bad idea but I think we should first find the best idea to solve the underlying pain point then talk about rejecting or supporting that.
>>>>
>>> You mean like this?
>>>
>>> https://docs.python.org/3/library/contextlib.html#contextlib.suppress
>> That doesn't do what the OP requested. It suppresses errors from the
>> outside but doesn't resume execution in the block so e.g.:
>>
>> a = b = None
>> with suppress(ValueError):
>>      a = float(str_a)
>>      b = float(str_b)
>>
>> The OP wants the the b= line to execute even if the a= line raises an exception.
>>
> True, but what the OP actually asked for is basically impossible. And
> at least you can write:
>
> with suppress(ValueError):
>      a = float(str_a)
> with suppress(ValueError):
>      b = float(str_b)
>
> which is a heap less noisy than the explicit try/except.

I think the OP was asking for a construct to automatically wrap every 
statement in a suppress context manager. Which is probably possible, but 
I think a bad idea. As a third party solution, maybe some creative AST 
manipulations could do the trick if someone were so inclined.

Eric



More information about the Python-ideas mailing list