| Store | Cart

[Python-Dev] (try-except) conditional expression similar to (if-else) conditional (PEP 308)

From: Nick Coghlan <ncog...@gmail.com>
Thu, 06 Aug 2009 20:47:45 +1000
P.J. Eby wrote:
> At 05:59 PM 8/5/2009 -0700, Raymond Hettinger wrote:>> [Jeffrey E. McAninch, PhD]>>> I very often want something like a try-except conditional expression>>> similar>>> to the if-else conditional.>>>>>> An example of the proposed syntax might be:>>>    x = float(string) except float('nan')>>> or possibly>>>    x = float(string) except ValueError float('nan')>>>> +1 I've long wanted something like this.>> One possible spelling is:>>>>   x = float(string) except ValueError else float('nan')> > I think 'as' would be better than 'else', since 'else' has a different> meaning in try/except statements, e.g.:> >    x = float(string) except ValueError, TypeError as float('nan')> > Of course, this is a different meaning of 'as', too, but it's not "as"> contradictory, IMO...  ;-)

(We're probably well into python-ideas territory at this point, but I'll
keep things where the thread started for now)

The basic idea appears sound to me as well. I suspect finding an
acceptable syntax is going to be the sticking point.

Breaking the problem down, we have three things we want to separate:

1. The expression that may raise the exception
2. The expression defining the exceptions to be caught
3. The expression to be used if the exception actually is caught

>From there it is possible to come up with all sorts of variants.

Option 1:

Change the relative order of the clauses by putting the exception
definition last:

  x = float(string) except float('nan') if ValueError
  op(float(string) except float('nan') if ValueError)

I actually like this one (that's why I listed it first). It gets the
clauses out of order relative to the statement, but the meaning still
seems pretty obvious to me.

Option 2:

Follow the lamba model and allow a colon inside this form of expression:

  x = float(string) except ValueError: float('nan')
  op(float(string) except ValueError: float('nan'))

This has the virtue of closely matching the statement syntax, but
embedding colons inside expressions is somewhat ugly. Yes, lambda
already does it, but lambda can hardly be put forward as a paragon of
beauty.

Option 3a/3b:

Raymond's except-else suggestion:

  x = float(string) except ValueError else float('nan')
  op(float(string) except ValueError else float('nan'))

This has the problem of inverting the sense of the else clause relative
to the statement form (where the else clause is executed only if no
exception occurs)

A couple of extra keywords would get the sense correct again, but I'm
not sure the parser could cope with it and it is rather verbose (I much
prefer option 1 to this idea):

  x = float(string) if not except ValueError else float('nan')
  op(float(string) if not except ValueError else float('nan'))

Option 4:

PJE's except-as suggestion:

  x = float(string) except ValueError as float('nan')
  op(float(string) except ValueError as float('nan'))

Given that we now use "except ValueError as ex" in exception statements,
the above strikes me a really confusing idea.

Cheers,
Nick.

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

Recent Messages in this Thread
Jeff McAninch Aug 05, 2009 10:22 pm
Raymond Hettinger Aug 06, 2009 12:59 am
P.J. Eby Aug 06, 2009 01:20 am
Nick Coghlan Aug 06, 2009 10:47 am
Dj Gilcrease Aug 06, 2009 11:18 am
MRAB Aug 06, 2009 11:39 am
Jeff McAninch Aug 06, 2009 02:36 pm
Dino Viehland Aug 06, 2009 09:55 pm
MRAB Aug 06, 2009 11:33 pm
Dino Viehland Aug 07, 2009 12:01 am
MRAB Aug 07, 2009 12:22 am
Jeff McAninch Aug 07, 2009 10:14 am
MRAB Aug 07, 2009 11:03 am
Dj Gilcrease Aug 10, 2009 02:29 pm
Jeff McAninch Aug 06, 2009 02:11 am
Antoine Pitrou Aug 06, 2009 11:32 am
Xavier Morel Aug 06, 2009 10:25 am
Russell E. Owen Aug 06, 2009 07:55 pm
MRAB Aug 07, 2009 12:36 am
Kristján Valur Jónsson Aug 07, 2009 10:22 am
ilya Aug 07, 2009 11:06 am
Michael Foord Aug 07, 2009 11:22 am
Alex Martelli Aug 07, 2009 02:55 pm
Steven DAprano Aug 08, 2009 06:02 am
Xavier Morel Aug 08, 2009 08:17 am
Stephen J. Turnbull Aug 08, 2009 01:19 pm
Alexander Kozlovsky Aug 14, 2009 10:41 pm
Steven DAprano Aug 10, 2009 11:45 pm
Messages in this thread