[Python-ideas] Making assert raise a different exception type

Steven D'Aprano steve at pearwood.info
Fri Oct 16 19:59:44 CEST 2009

On Sat, 17 Oct 2009 04:03:38 am Oleg Broytman wrote:
> On Fri, Oct 16, 2009 at 09:52:49AM -0700, Guido van Rossum wrote:
> > if not some_false_condition:
> if __debug__ and not some_false_condition...

That's not quite the same as assert. With assert, the test of __debug__ 
happens at compile time, not runtime. With the above, it happens at 
runtime (at least in Python 2.6).

To get the same behaviour as assert, you would need something like:

if __debug__:
    if not some_condition:
        raise ValueError(msg)

Steven D'Aprano

More information about the Python-ideas mailing list