[Python-ideas] except expression

Phil Connell pconnell at gmail.com
Thu Feb 20 09:21:53 CET 2014


On Thu, Feb 20, 2014 at 12:48:33PM +1100, Chris Angelico wrote:
> On Thu, Feb 20, 2014 at 12:11 PM, Yury Selivanov
> <yselivanov.ml at gmail.com> wrote:
> >
> > On 2/19/2014, 7:56 PM, Chris Angelico wrote:
> >>
> >> On Thu, Feb 20, 2014 at 11:22 AM, Yury Selivanov
> >>> value = 42 if a[10] raises
> >> My main objection here is that it reads backwards.
> >
> > Yes, that's understandable.
> >
> > Although, as you yourself noted, Python has
> > 'expr if expr else expr' already. Yes, it is a bit awkward,
> > but lots of people (including me) find it very readable and
> > easy to understand.
> 
> Yes, but is that a good thing? The ternary if is justified because it
> reads well in English, but even so, it does trap people. I am
> listening to proposals that put the operands in the "wrong order", but
> the bar is higher - they have to justify that potential confusion.

Moreover the analogy with the if expression is completely bogus:

    value = <expr on exception> if <expr on success> raises

is the *opposite* way round to
    
    value = <expr if true> if <cond> else <expr if false>


In the if expression, the mainline value comes first, the exceptional value
comes last.

Yury's proposal has it the other way round, exceptional value first.



More information about the Python-ideas mailing list