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

Steven D'Aprano steve at pearwood.info
Tue May 8 11:39:49 EDT 2018


On Tue, May 08, 2018 at 09:58:51AM -0400, Eric V. Smith wrote:

> I think what's confusing to me (and maybe others) is that we haven't 
> seen your vision on how this would look in Python code.
> 
> An example that would throw runtime assertions and the same example 
> where it wouldn't (after a global switch is set?) would be helpful.

In Eloi's first post in this thread, he gave the example:

    runtime_assert( expr )


Although it is written as a function call, he refers to it as a 
statement, so I guess he means:

    runtime_assert expr


He says that would compile to the equivalent of:

    if runtime_assert_active and expr:
        print(RuntimeAssertionError())


but he gives no idea of what runtime_assert_active is (is it a 
per-module global variable? a single application-wide super-global? a 
local variable? something else?) or how we are supposed to set it.

Nor does he explain why failed assertions merelt *print* an error 
message, rather than raising an exception.



-- 
Steve


More information about the Python-ideas mailing list