1

I'm just curious if this exists. After programming python for the better part of a year I've never come across it.

Is there a python function that is c-compiled (for faster access in comprehensions) that checks an exception:

A function like the following:

def no_exception(function, *args, **kwargs):
    try:
        function(*args, **kwargs)
    except Exception:
        return False
    return True

You could use it in this case

# values is full of data
new_values = [float(n) if no_exception(float, n) else n for n in values]
Wooble
  • 87,717
  • 12
  • 108
  • 131
Garrett Berg
  • 2,585
  • 1
  • 22
  • 21

1 Answers1

1

No

at least not in the standard library. Otherwise the assertRaises method in the Python unittest module would use it. See: http://pythonhosted.org/gchecky/unittest-pysrc.html#TestCase.failUnlessRaises

You can of course write your own c implementation easily.

Bernhard Kausler
  • 5,119
  • 3
  • 32
  • 36
  • Thanks for the response! For some reason I was pretty convinced that I had missed something, but looks like I didn't :) – Garrett Berg Mar 18 '13 at 16:52