On 28.11.2017 03:28, Ivan Pozdeev via Python-ideas wrote:
On 28.11.2017 5:19, Chris Angelico wrote:
Actually, Python does have a way of disabling assertions (the -O flag), so they should be treated the same way they are in C. Assertions should not be used as shorthands for "if cond: raise Exc" in the general case.
I'm claiming, and provided evidence, that there are no use cases for this in Python, so no-one (of any significance) will suffer when the disabling is cut out.
... except those who have relied on this behavior for around two decades to make their code run faster in production environments and those who run on memory constrained systems using -OO to remove doc-strings as well.
asserts are meant as debug tool and runtime way to document application internal expectations of code following them. Once all tests pass they are no longer needed.
If you find that you still need them, you should recode the asserts as proper if statements.
As example use case, asserts testing user provided data are not a good idea, since you cannot test all possible user inputs. For those cases, an if statement is the right way to implement your checks.