| Store | Cart

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

From: Jeff McAninch <mcan...@lanl.gov>
Wed, 05 Aug 2009 16:22:30 -0600
I'm new to this list, so please excuse me if this topic has been 
discussed, but I didn't
see anything similar in the archives.

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')

Here's a simple example: Converting a large list of strings to floats 
where there may be errors
that I want returned as nan's.

Currently I would write the function:
    def safe_float_function(string):
        try:
            result = float(string)
        except:
            result = float('nan')
        return result
and get my list of floats using the list comprehension:
    xs = [ safe_float_function(string) for string in strings ]

With a try-except conditional I would instead define the following lambda:
    safe_float_conditional = lambda string : float(string) except 
float('nan')
leading to:
    xs = [ safe_float_conditional(string) for string in strings ]

My understanding is that the second would be faster at run time, and, 
like if-else conditional expressions,
possibly more easily read by the human.

Again, please excuse me if this has been discussed previously.  If so, 
I'd appreciate being pointed to the discussion.

Please also excuse me if for there is some currently (pre-python 3.0) 
idiom that I could use to efficiently get this
same behaviour.  If so, I'd appreciate being educated.

Thanks,
Jeff McAninch

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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20090805/2d0ea125/attachment.htm>

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