[Python-ideas] Repurpose `assert' into a general-purpose check

Stephen J. Turnbull turnbull.stephen.fw at u.tsukuba.ac.jp
Tue Nov 28 03:54:12 EST 2017


Ivan Pozdeev via Python-ideas writes:

 > The `assert' statment was created the same as in previous languages like 
 > C/C++: a check to only do in debug mode, when you can't yet trust your 
 > code to manage and pass around internal data correctly. Examples are 
 > array bounds and object state integrity constraints.

I use assert in Python as I use it in C: as a statement that something
should NEVER happen, algorithmically.  I do occasionally have
expensive assertions in inner loops, and do use -O to disable them
when I assess that speed is important enough to offset the increased
risk.

So, -1.

Oh, yeah: I have had programs stop because I got the algorithm wrong,
and because I implemented it incorrectly.

I understand the point made elsewhere about scientists' thinking about
"assert", but as a scientist myself, I disagree.  It is very useful to
me to distinguish between validating data and validating algorithms.
"assert" is how we do the latter.




More information about the Python-ideas mailing list