[Python-ideas] except expression

Nick Coghlan ncoghlan at gmail.com
Wed Feb 19 13:17:05 CET 2014


On 19 February 2014 17:53, Paul Moore <p.f.moore at gmail.com> wrote:
> On 19 February 2014 07:34, Chris Angelico <rosuav at gmail.com> wrote:
>> Have you a preferred keyword-based proposal? Does it use an existing
>> keyword or create a new one? The floor is yours, Paul: sell me your
>> keyword :) Bear in mind, I used to be in favour of the "expr except
>> Exception pass default" form, so it shouldn't be too hard to push me
>> back to there.
>
> Honestly? No, I don't. If I need an example of non-colon syntax I
> choose "return" and consider "pass" because those are the 2 that seem
> most reasonable, and I remember "return" fastest. But I can't really
> argue strongly for them - whoever said it was right, *all* of the
> keyword approaches are picking "something that's not awful" from what
> we have.
>
> My list of new keywords is basically the same as yours, but I don't
> think the construct is important enough to warrant a new keyword (if
> the if-expression couldn't justify "then", then we don't stand a
> chance!)
>
> So having struggled to find objections to the colon syntax, I've
> reached a point where I think it's the best of the alternatives. "I
> can't find a good enough objection" isn't exactly a ringing
> endorsement, but it's the best I've got :-)

Right, unless Guido manages to pull another PEP 308 style "I have come
up with a clever idea nobody else thought of, and likely only the BDFL
could sell to anyone else", I think the colon based version Chris has
written up in the PEP currently counts as "least bad of the
alternatives proposed, and sufficiently tolerable to be judged better
than the assortment of relatively ad hoc workarounds we have at the
moment".

That said, I did suggest using the function return type annotation
marker as a possibility, and that's not currently listed in the PEP:

    value = lst[2] except IndexError -> 'No value'

I do slightly prefer that to the colon based version, although we may
want to go with the generator expression approach of always requiring
parentheses around it (with function call parentheses counting):

    value = (lst[2] except IndexError -> 'No value')


On an unrelated tangent (as I forget which part of the thread it came
up in), having to switch from the compiler checked and code completion
friendly 'obj.attr' to the more opaque (from an automated code
analysis point of view) 'getattr(obj, "attr", None)' is another
symptom of this current limitation that should be listed in the
motivation section of the PEP.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list