Why assert is not a function?

Serhiy Storchaka storchaka at gmail.com
Thu Mar 11 16:20:26 EST 2021


11.03.21 20:31, Chris Angelico пише:
>> assert(expensive_computation())
> 
> Do you have any asserts like that, or is that a purely theoretical
> complaint? I have never once seen anything that costly - usually it'll
> be something like checking a length (and this isn't C's strlen, since
> Python can get lengths of all built-in types quickly), or some simple
> checks.

I have a lot of asserts when use other programming languages. Even
simple bound check for index can be too expensive for optimized build.
It is less common in Python because it is less common to use -O option
in Python. If most users do not bother to use -O option, you do not want
to make your library slower for them by adding runtime self-checks. It
is not possible to enable optimization on per-package level.



More information about the Python-list mailing list