[Python-ideas] Inline 'raises' expression

Fredrik Johansson fredrik.johansson at gmail.com
Mon Aug 25 15:55:25 CEST 2008

On Sun, Aug 24, 2008 at 4:01 PM, Paul Hankin <paul.hankin at pobox.com> wrote:
> There's not much need for a language modification; you can code it up
> directly. The obvious problem with what you want is that it's only
> good if you want to catch a single exception.
> def raises(thunk, exn):
>    try:
>        thunk()
>        return False
>    except exn:
>        return True

True, but that's a lot of noise for something very simple. And the
extra required "lambda:" makes the "raises" expression much less

On Mon, Aug 25, 2008 at 1:47 AM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Fredrik Johansson wrote:
>> one could just write
>>    assert 1/0 raises ZeroDivisionError
> It's been pointed out that using assert for testing
> is not the best idea, since then you can't test that
> the code works properly with assertions turned off.

Good point, though I've never personally felt the need to run Python
with assertions turned off. (It would arguably be more useful if
assertions were enabled on a per module basis, but this is a
tangential matter.)


More information about the Python-ideas mailing list