[Python-ideas] except expression
MRAB
python at mrabarnett.plus.com
Tue Feb 18 03:11:47 CET 2014
On 2014-02-18 00:48, Chris Angelico wrote:
> On Tue, Feb 18, 2014 at 11:20 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
>> On 02/17/2014 03:54 PM, Chris Angelico wrote:
>>> 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.
>
> Ooh. That's an interesting one. I'm not sure how that would go in
> terms of readability, but it is an interesting concept. Very
> interesting concept. Not sure that I like it, but ... the mind does
> like the idea ... in a weird and dangerous way.
>
> def throw(ex):
> raise ex
>
> some_func(1, 2, (x if x is not None else throw(SyntaxError)) except
> SyntaxError: pass)
>
> So if x is None, it doesn't get passed at all. Useful feature... wordy
> and clunky spelling... no, I'm now sure that I do not like this. But
> it's amusing!
>
That would be clearer as:
some_func(1, 2, (x if x is not None else pass))
for some value of "clearer". :-)
More information about the Python-ideas
mailing list