[Python-ideas] except expression

Chris Angelico rosuav at gmail.com
Tue Feb 18 01:59:20 CET 2014


On Tue, Feb 18, 2014 at 11:50 AM, Rob Cliffe <rob.cliffe at btinternet.com> wrote:
>
> On 17/02/2014 22:26, Greg Ewing wrote:
>> There's no logical difference between not generating
>> a result at all, and generating a result of None and
>> then throwing it away.
>>
>> The same thing applies here:
>>
>>    menu.remove(mint) except ValueError: pass
>
> Yes, this does read better than (say) "except ValueError: None" or even
> "except ValueError: doNothing()".
> I'm starting to like it,  although it introduces an inconsistency between
> expressions whose values are used and those whose values are not used.
>
>> This says exactly what the programmer means: "Remove
>> mint from the menu if it's there, otherwise do nothing."
>>
>> Either way, this would be allowed *only* for top
>> level expressions whose value is ignored.
>
> Yes of course, otherwise it would make no sense.

The trouble with this is that it blurs the line between a statement
and an expression. Normally, "menu.remove(mint)" is an expression, it
has a value. In the interactive interpreter, any non-None value
returned will be printed. Adding "except ValueError: pass" to the
end... might make it not an expression any more. Or does it always
make it not an expression?

This can be spelled:
    try: f()
    except ValueError: pass

which is only two lines (down from four for a "classic" try block).
I'm not sure that going down to one is worth the confusion. You can
keep it as an expression by just returning None, or you can make it a
statement in two lines.

But if you like, I can add another section to the PEP mentioning this.

ChrisA


More information about the Python-ideas mailing list