[Python-ideas] exception based conditional expression, similar to if-else conditional expression
stefan_ml at behnel.de
Fri Aug 21 14:16:53 CEST 2009
Chris Rebert wrote:
> On Thu, Aug 20, 2009 at 3:15 AM, Jeff McAninch<mcaninch at lanl.gov> wrote:
>> 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')
> I would feel more comfortable with another keyword in there. Perhaps:
> x = float(string) except ValueError then float('nan')
> Which reads like:
> "x is the float value of string, except in the event of ValueError,
> then it's float('nan')"
> Which I find rather pleasing.
I'm -1 on this. I think it's of much rarer use than even conditional
expressions, has the disadvantage of only handling one special use case
without being easily extensible to more than one exception, and it's easily
served by a simple function that does the same thing (and potentially much
more) but gives it a name.
More information about the Python-ideas