I assume that the main reason assert not used most often is that no one uses Python optimized mode .
Asserts is a great tool for detecting programming errors, to protect against unforeseen situations, but all this error checking is cost-related. In compiled languages such as C / C ++, this is not a big deal, since statements are only included in debug builds and are completely removed from releases.
In Python, on the other hand, there is no strict difference between debug and release mode. The interpreter has an "optimization flag" ( -O ), but currently it does not actually optimize the byte code, but only removes the statements.
Therefore, most Python users simply ignore the -O flag and run their scripts in "normal mode", which is a kind of debugging mode, since the statements are enabled, and __debug__ is True , but it is considered "production" ready.
Maybe it would be wiser to switch logic, i.e. “optimize” by default and enable only permissions in explicit debugging mode (*), but I assume that this will confuse many users, and I doubt that we will ever see such a change.
((*) This, for example, is how the Java VM does this using the -ea (enable assertions) switch.)
Ferdinand Beyer Feb 03 2018-12-12T00: 00Z
source share