[Python-ideas] except expression

Rob Cliffe rob.cliffe at btinternet.com
Tue Feb 18 01:50:49 CET 2014


On 17/02/2014 22:26, Greg Ewing wrote:
> 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
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."
>
> 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.
Yes of course, otherwise it would make no sense.
Rob Cliffe
> In any
> context where the value of the expression is used for
> something, the exceptional value would have to be
> spelled out explicitly.
>



More information about the Python-ideas mailing list