[Python-ideas] except expression
Paul Moore
p.f.moore at gmail.com
Tue Feb 18 16:56:10 CET 2014
On 18 February 2014 15:05, Chris Angelico <rosuav at gmail.com> wrote:
>> Well, yes and no. There are only 2 out of the 10 syntaxes originally
>> proposed in the PEP at the start of this thread that use a colon. I've
>> already pointed out that I don't like a colon, so assume I said
>> "except return 0" if you must :-)
>
> Whether it's a colon or another keyword, _something_ is necessary in
> there, if it's permissible to specify an exception type:
>
> sum(x[3] except IndexError 0 for x in list_of_tuples)
> sum(x[3] except 0 for x in list_of_tuples)
>
> How would you parse each of those, without a separator?
With "return" as a separator. I said that above. Was I not clear?
> One option might be to have a separator that's present only when the
> exception type is listed. For instance:
>
> sum(x[3] except(IndexError) 0 for x in list_of_tuples)
> sum(x[3] except 0 for x in list_of_tuples)
>
> Does that sort of thing have enough support to be added to the PEP?
> I'm not sure it gives us anything over the colon notation, which has
> the benefit of being consistent with the statement form (and is
> stylistically similar to lambda, so it's not grossly inappropriate to
> an expression context)
I remain less than happy with the colon notation, although I will
concede that the basic
x[3] except IndexError: 0
form is not horrible - it just becomes horrible very, very fast when
people try to do anything more complicated than that. On the other
hand, none of the examples of *any* of the proposed syntaxes look
compelling to me - they are either too simple to be realistic, or so
complex that they should be factored out into multiple statements, in
my view.
Paul
More information about the Python-ideas
mailing list