Disable statements in Python

How to disable statements in Python?

That is, if the statement fails, I do not want it to select AssertionError , but keep going.

How to do it?

+73
python debugging assert exception-handling environment-variables
Aug 13 '09 at 16:49
source share
6 answers

How to disable statements in Python?

There are several approaches that affect one process, environment, or one line of code.

I show everyone.

For the whole process

Using the -O flag (capital O) disables all assert statements in the process.

For example:

 $ python -Oc "assert False" $ python -c "assert False" Traceback (most recent call last): File "<string>", line 1, in <module> AssertionError 

Note that by disconnecting, I mean that it also does not execute the expression following it:

 $ python -Oc "assert 1/0" $ python -c "assert 1/0" Traceback (most recent call last): File "<string>", line 1, in <module> ZeroDivisionError: integer division or modulo by zero 

For the environment

You can also use the environment variable to set this flag.

This will affect every process that uses or inherits the environment.

For example, on Windows, install and then clear the environment variable:

 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 

Same thing on Unix (using set and unset for related functionality)

One point in code

You continue your question:

if the statement fails, I do not want it to throw an AssertionError error, but continue.

If you want the code to not execute, you can make sure that the control flow does not reach the statement, for example:

 if False: assert False, "we know this fails, but we don't get here" 

or you may find a confirmation error:

 try: assert False, "this code runs, fails, and the exception is caught" except AssertionError as e: print(repr(e)) 

which prints:

 AssertionError('this code runs, fails, and the exception is caught') 

and you continue from where you worked with AssertionError .

References

From the assert documentation :

An assertion statement like this:

 assert expression #, optional_message 

Equivalent

 if __debug__: if not expression: raise AssertionError #(optional_message) 

AND,

the __debug__ built-in variable is True under normal conditions, False when optimization is requested (command line option -O ).

and further

__debug__ are illegal. The value for the built-in variable is determined when the interpreter starts.

From the usage documentation:

-O

Turn on core optimizations. This changes the file name extension for compiled (bytecode) files from .pyc to .pyo. See also PYTHONOPTIMIZE.

and

PYTHONOPTIMIZE

If it is not an empty string, it is equivalent to specify the -O option. If an integer is specified, it is equivalent to specifying -O several times.

+49
Apr 27 '17 at 21:50
source share

Python call with the -O flag:

test.py:

 assert(False) print 'Done' 

Output:

 C:\temp\py>C:\Python26\python.exe test.py Traceback (most recent call last): File "test.py", line 1, in <module> assert(False) AssertionError C:\temp\py>C:\Python26\python.exe -O test.py Done 
+57
Aug 13 '09 at 16:53
source share

Both of the two answers already provided are valid (invoke Python using -O or -OO on the command line).

Here is the difference between the two:

  • -O Turn on basic optimizations. This changes the file name extension for compiled (bytecodes) files from .pyc to .pyo.

  • -OO Cancel docstrings in addition to optimizing -O .

(from Python documentation )

+14
Jan 24 '14 at 5:45
source share

Use python -O :

 $ python -O >>> assert False >>> 
+7
Aug 13 '09 at 16:52
source share

You must NOT disable (most) statements. They catch unforeseen errors when attention is elsewhere. See Rule 5 in Power of Ten .

Instead, protect some expensive approval checks with something like:

 import logging logger = logging.getLogger(__name__) if logger.getEffectiveLevel() < logging.DEBUG: ok = check_expensive_property() assert ok, 'Run !' 

One way to preserve important statements and enable optimization of assert is to make them in a choice expression:

 if foo_is_broken(): raise AssertionError('Foo is broken!') 
+4
Apr 20 '15 at 7:41
source share

Running in optimized mode should do this:

 python -OO module.py 
+2
Aug 13 '09 at 16:52
source share



All Articles