[Python-ideas] except expression
Greg Ewing
greg.ewing at canterbury.ac.nz
Mon Feb 17 23:26:41 CET 2014
Chris Angelico wrote:
> On Mon, Feb 17, 2014 at 1:47 PM, Ethan Furman <ethan at stoneleaf.us> wrote:
>
>>def have_a_mint(some, args, here):
>> # flesh this out later
>> pass
>>
>>Does anybody really think that that function will not return None?
>
> Of course it'll return None, but that's nothing to do with the 'pass'.
> The keyword 'pass' doesn't generate any return result at all.
Function return values are a red herring here. The
point is to allow the programmer to directly express
the intent of the code without having to introduce
spurious values that will be ignored.
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."
An alternative would be to allow the exceptional
value part to be omitted altogether:
menu.remove(mint) except ValueError
but that looks incomplete, making you wonder if the
programmer forgot something.
Either way, this would be allowed *only* for top
level expressions whose value is ignored. In any
context where the value of the expression is used for
something, the exceptional value would have to be
spelled out explicitly.
--
Greg
More information about the Python-ideas
mailing list