Actually the whole question in the title.

If there is no such possibility, I don’t understand what is the value of such an instruction as assert in general?

And somehow everything is unconvincing. After all, you can do with simple if ...: print() ...
PS: I have already received a response from andreymal , however it is interesting to disable assert first line in the code ... is it possible? After all, os.environ is read to the first line of the module ...

  • 2
    In general, for me personally, assert are 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 rewrite assert as if , then the one who reads the code may decide that the condition violation is a normal situation in the program, and assert s are intended for catching exactly abnormal and impossible situations - bugs. And I do not care if it is possible to disable assert or not - for me, this is primarily a way of organizing order in the code and simplifying its reading and debugging. - andreymal
  • “Disable assert as the first line in the code” - but this is hardly possible - andreymal
  • Also, to change the essence of the question after it is answered is not very good - andreymal
  • one
    In general, the most likely on-off debugging is determined long before the launch of any modules at all. When compiling, the code assert and __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

1 answer 1

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

  • except AssertionError looks like an anti-pattern. assert indicates a bug in the code (no input: neither correct nor erroneous should not lead to AssertionError) What use-case do you represent, in which it is useful? - jfs
  • @jfs a crutch to work around a problem in a third-party library?)) In general, this is a translation and I did not come up with AssertionError to intercept, it would be better to ask this question to the original author on enSO (but added an answer) - andreymal
  • one
    Blindly do not copy the answers. Aside: the author of the original deletes the comments to his answers, without addressing (in the opinion of the author of the commentary) the problems noted in the commentary. - jfs