[Python-ideas] except expression

MRAB python at mrabarnett.plus.com
Tue Feb 18 01:57:28 CET 2014


On 2014-02-18 00:20, Ethan Furman wrote:
> On 02/17/2014 03:54 PM, Chris Angelico wrote:
>> On Tue, Feb 18, 2014 at 9:26 AM, Greg Ewing <greg.ewing at canterbury.ac.nz> 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
>>>
>>> This says exactly what the programmer means: "Remove
>>> mint from the menu if it's there, otherwise do nothing."
>>
>> In a statement context, it's possible to say "otherwise do nothing".
>> An if without an else does this, as does putting 'pass' in certain
>> places.
>>
>> But in an expression context, there are 'two possibility'. Either some
>> kind of value is returned, or an exception is raised. (At least, I
>> don't think there are any other options. Harry Hoo, not the Spanish
>> Inquisition.) What should happen here:
>>
>> func(menu.remove(mint) except ValueError: pass)
>>
>> If remove() raises ValueError, should func be called? If so, with what
>> argument? (Note that it's because English is happy with "passing"
>> arguments to functions that I was happy with the notation "except
>> ValueError pass None", but that's using pass as a syntactic element,
>> in place of the colon.)
>
> Yes, func should be called, and it should be called with no arguments.
>
In that case, is:

     func(foo if condition else pass)

allowed (being like "func(foo) if condition else func()")?

And, in fact, is:

     func(pass)

also allowed (meaning "func()")?



More information about the Python-ideas mailing list