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

Jim Jewett jimjjewett at gmail.com
Thu Aug 20 16:25:06 CEST 2009


On Thu, Aug 20, 2009 at 7:18 AM, Jeff McAninch<mcaninch at lanl.gov> wrote:
> The bang-for-the-buck in the exception expression
> is in performance. ...

> ... doesn't a call to a python function
> (as opposed to a compiled C-function) substantially
> slow down a list comprehension?

Probably, but you don't have to use a comprehension.

def g(seq):
    for e in seq:
        try:
            yield float(e)
        except:
            pass # whatever, even skipping the element

If you don't need it in generator form, then just collect the results
into a list and return that instead of yielding.  (And obviously, that
for loop doesn't even have to be in a separate (generator or) function
at all.)

-jJ



More information about the Python-ideas mailing list