[Python-ideas] [Fwd: Re: exception based conditional expression, similar to if-else conditional expression]

Jeff McAninch mcaninch at lanl.gov
Thu Aug 20 12:15:28 CEST 2009

Calvin Spealman wrote:

> -1 on colons in the expression like that. I like the idea of being
> able to handle an exception in generator expressions and the like, but
> I've never seen a syntax I liked. I think I've favored the idea of
> something like `float(x) except float('nan') if ValueError` thinking
> it reads more naturally as an expression, puts the real logic
> ("convert x to a float or get a NaN float") together, which I think
> makes sense.

Yes, I agree about the colons.  They have no purpose.  I was just 
blindly following the try-except.  (Duh! on my part)

So, in the simple example:
   x = float(string) except ValueError float('nan')

But possibly the exception tuples now have to be explicitly tuples?
   x = float(string) except (ValueError,) float('nan')

So the general case would be something like
   exception_expression :== nominal_value {except exception_tuple exception_value}* {except default_value}

Jeff McAninch

Jeffrey E. McAninch, PhD
Physicist, X-2-IFD
Los Alamos National Laboratory
Phone: 505-667-0374
Email: mcaninch at lanl.gov

More information about the Python-ideas mailing list