assert , unlike if , is designed to detect situations that were conceived as basically impossible in the program: to search for bugs. Disabling assert usually not worth it, but it can sometimes be useful to speed up the program.
There are several ways to disable it.
For a separate Python process
The use of the -O flag (large Latin O) enables basic optimization and disables all assert in this process.
Example:
$ python -Oc "assert False" $ python -c "assert False" Traceback (most recent call last): File "<string>", line 1, in <module> AssertionError
For the environment
You can use the environment variable to set this flag. Then it will be applied to all processes using this environment.
For example, setting and clearing the environment variable in Windows:
C:\>python -c "assert False" Traceback (most recent call last): File "<string>", line 1, in <module> AssertionError C:\>SET PYTHONOPTIMIZE=TRUE C:\>python -c "assert False" C:\>SET PYTHONOPTIMIZE= C:\>python -c "assert False" Traceback (most recent call last): File "<string>", line 1, in <module> AssertionError
For a specific place in the code
When an expression specified in assert is false, an AssertionError exception is thrown. If such an assert is expected to fail, you can simply catch this exception:
>>> try: ... assert False, "мы знаем, что это упадёт" ... except AssertionError as e: ... print(repr(e)) ... AssertionError('мы знаем, что это упадёт',)
After such an interception exception, if you do not throw a new exception, the program will continue to run.
(However, doing so badly: if assert fails, you need to take all measures to correct the program so that it does not fail any more, and not hide the possible bug with such a crutch.)
Additional Information
From the assert documentation :
An expression with assert , like this:
assert expression #, optional_message
Equivalent to this code:
if __debug__: if not expression: raise AssertionError #(optional_message)
AND
The built-in variable __debug__ is True under normal conditions and False if optimization is enabled (the -O command line argument).
From the python usage documentation:
-O
Includes basic optimizations. See also PYTHONOPTIMIZE .
and
PYTHONOPTIMIZE
If this variable is a non-empty string, this is analogous to using the -O option. If an integer is specified in a variable, this is the same as adding the -O option several times.
Slightly free translation of the answer from Aaron Hall to enSO
assertare useful in that they clearly demonstrate to the reader of the code that certain conditions in the program are basically impossible (or at least thought impossible). If you rewriteassertasif, then the one who reads the code may decide that the condition violation is a normal situation in the program, andasserts are intended for catching exactly abnormal and impossible situations - bugs. And I do not care if it is possible to disableassertor not - for me, this is primarily a way of organizing order in the code and simplifying its reading and debugging. - andreymalassertand__debug__are cut out from bytecode (when debugging is turned on, they are replaced with equivalent expressions, when disabled, they are removed completely) (correct me if it is not right), and it becomes technically impossible to turn on / off debugging while the program is running. Therefore, the only thing that remains for you is to somehow adapt one of the methods listed in the answer for your situation. - andreymal