[Python-ideas] Runtime assertion with no overhead when not active

Eloi Gaudry Eloi.Gaudry at fft.be
Sat May 5 08:54:25 EDT 2018

I meant avoiding the overhead of the expression evaluation enclosed in the assert itself, not the active/disabled state (that comes at virtually no cost).
>>> runtime_assert( { i:None for i in range( 10000000 ) } )  

By using the syntax you describe ('boolean and not expr'), you loose all the benefit of the Python grammar:
- self-contained
- able to catch semantic/syntax bugs

>>> f=1
>>> runtime_assert( f==1 )
>>> runtime_assert( f=1 )
  File "<stdin>", line 1
    runtime_assert( f=1 )
SyntaxError: invalid syntax

-----Original Message-----
From: Python-ideas <python-ideas-bounces+eloi.gaudry=fft.be at python.org> On Behalf Of Serhiy Storchaka
Sent: Saturday, May 5, 2018 2:05 PM
To: python-ideas at python.org
Subject: Re: [Python-ideas] Runtime assertion with no overhead when not active

05.05.18 11:04, Eloi Gaudry пише:
>     Briefly, the idea is to add a new assert that can be switch on/off
>     depending on some variable/mechanism at runtime. The whole point of
>     this assert is that it should not bring any overhead when off, i.e.
>     by avoiding evaluating the expression enclosed in the runtime
>     assert. It thus relies on Python grammar.

You should have an overhead for checking if it is switched on/off, isn't? And this overhead is virtually the same as testing the value of the global boolean variable.

> runtime_assert( expr )
> #would result in if expr and runtime_assert_active:
> print RuntimeAssertionError()

How this will be different from

     if debug and not expr:


Python-ideas mailing list
Python-ideas at python.org
Code of Conduct: http://python.org/psf/codeofconduct/

More information about the Python-ideas mailing list