[Python-ideas] Inline 'raises' expression

Fredrik Johansson fredrik.johansson at gmail.com
Sun Aug 24 12:43:59 CEST 2008


Hi,

It happens that I'm just interested in whether an expression raises an
exception, not the return value. This might look something like

    try:
        check_status()
    except IOError:
        cleanup()

which could be written more simply as

    if check_status() raises IOError:
        cleanup()

Also, instead of the inconvenient

    self.assertRaises(ZeroDivisionError, lambda: 1/0)

one could just write

    assert 1/0 raises ZeroDivisionError

Something like this would especially be useful for those of us who
aren't fans of the unittest framework. Alternatively, just the
assert--raises form could be permitted.

Thoughts?

Fredrik



More information about the Python-ideas mailing list