Another use for the -O flag is that the value of the __debug__ builtin variable is set to False.

So, basically, your code can have a lot of "debugging" paths like:

if __debug__:
     # output all your favourite debugging information
     # and then more

which, when running under -O, won't even be included as bytecode in the .pyo file; a poor man's C-ish #ifdef.

Remember that docstrings are being dropped only when the flag is -OO.

Answer from tzot on Stack Overflow
🌐
GitHub
github.com › lark-parser › lark › issues › 185
Usage of assert statements in optimized mode · Issue #185 · lark-parser/lark
July 13, 2018 - I did a quick experiment, and running the following like of code fails in both normal and optimized mode: lark.Lark(grammar='start: "my_token"', parser='not_a_parser') But the yielded exception is different. Normal mode is: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/my_user/opensource/lark/lark/lark.py", line 108, in __init__ self.options = LarkOptions(options) File "/home/my_user/opensource/lark/lark/lark.py", line 68, in __init__ assert self.parser in ('earley', 'lalr', 'cyk', None) AssertionError
Author   petermlm
Discussions

pytest, assert keyword and optimized mode
I find using assert keyword and pytest/pytest-django much more Pythonic and elegant for writing tests, that’s why I’ve used factory-boy, pytest-django, Faker and django-test-plus packages to write tests, for the first time. However, when I shared my public repo (https://github.com/Ksen... More on forum.djangoproject.com
🌐 forum.djangoproject.com
1
0
January 13, 2022
Improve the assert statement documentation to clarify impact of -O and optimization
The existing documentation states, "The current code generator emits no code for an assert statement when optimization is requested at compile time." This could include more details that setting the -O flag on the Python executable, or setting the PYTHONOPTIMIZE environment variable, will disable ... More on github.com
🌐 github.com
3
February 8, 2024
tests should not use a Python `assert` statement but instead use `numpy.testing.assert_`
We still use assert in our test cases. The Python assert statement is optimized away with python -O, in which case some of the tests become useless. I don't think that tests are run with -O but bet... More on github.com
🌐 github.com
4
February 15, 2016
python - Why assert is not largely used? - Stack Overflow
This is why you usually need to ... use the "optimized" mode. 2021-01-22T13:55:49.8Z+00:00 ... Why rise of type hints among pythonistas (yes, my comment is 10y+ after accepted answer!) and static type checkers increasing usage, assert could have a greater use. (for instance as described by the great @erictraut github.com/micro... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
github.com › python › cpython › blob › main › Python › optimizer_analysis.c
cpython/Python/optimizer_analysis.c at main · python/cpython
assert(out < ctx->out_buffer.end); out->opcode = (opcode); out->format = this_instr->format; out->oparg = (oparg); out->target = this_instr->target; out->operand0 = (operand0); out->operand1 = this_instr->operand1; ctx->out_buffer.next++; } · /* Shortened forms for convenience, used in optimizer_bytecodes.c */ #define sym_is_not_null _Py_uop_sym_is_not_null ·
Author   python
🌐
GitHub
github.com › python › cpython › issues › 115177
Improve the assert statement documentation to clarify impact of -O and optimization · Issue #115177 · python/cpython
February 8, 2024 - The existing documentation states, "The current code generator emits no code for an assert statement when optimization is requested at compile time." This could include more details that setting the -O flag on the Python executable, or setting ...
Author   davidlbaird
🌐
GitHub
github.com › MDAnalysis › mdanalysis › issues › 724
tests should not use a Python `assert` statement but instead use `numpy.testing.assert_` · Issue #724 · MDAnalysis/mdanalysis
February 15, 2016 - We still use assert in our test cases. The Python assert statement is optimized away with python -O, in which case some of the tests become useless. I don't think that tests are run with -O but better do it right because there's the pote...
Author   orbeckst
Top answer
1 of 5
83

I guess the main reason for assert not being used more often is that nobody uses Python's "optimized" mode.

Asserts are a great tool to detect programming mistakes, to guard yourself from unexpected situations, but all this error checking comes with a cost. In compiled languages such as C/C++, this does not really matter, since asserts are only enabled in debug builds, and completely removed from release builds.

In Python, on the other hand, there is no strict distinction between debug and release mode. The interpreter features an "optimization flag" (-O), but currently this does not actually optimize the byte code, but only removes asserts.

Therefore, most Python users just ignore the -O flag and run their scripts in "normal mode", which is kind of the debug mode since asserts are enabled and __debug__ is True, but is considered "production ready".

Maybe it would be wiser to switch the logic, i.e., "optimize" by default and only enable asserts in an explicit debug mode(*), but I guess this would confuse a lot of users and I doubt we will ever see such a change.

((*) This is for example how the Java VM does it, by featuring a -ea (enable assertions) switch.)

2 of 5
38

Several reasons come to mind...

It is not a primary function

Many programmers, lets not get bogged down by the rationale, disrespect anything which is not a direct participant in the program's penultimate functionality. The assert statement is intended for debugging and testing, and so, a luxury they can ill-afford.

Unit Testing

The assert statement predates the rise and rise of unit-testing. Whilst the assert statement still has its uses, unit-testing is now widely used for constructing a hostile environment with which to bash the crap out of a subroutine and its system. Under these conditions assert statements start to feel like knives in a gunfight.

Improved industry respect for testing

The assert statement serves best as the last line of defence. It rose to lofty and untouchable heights under the C language, when that language ruled the world, as a great way to implement the new-fangled "defensive programming"; it recognises and traps catastrophic disasters in the moment they teeter on the brink. This was before the value of Testing became widely recognised and respected and disasters were substantially more common.

Today, it is unheard of, for any serious commercial software to be released without some form of testing. Testing is taken seriously and has evolved into a massive field. There are Testing professionals and Quality Assurance departments with big checklists and formal sign-offs. Under these conditions programmers tend not to bother with asserts because they have confidence that their code will be subjected to so much tiresome testing that the odds of wacky brink-of-disaster conditions are so remote as to be negligible. That's not to say they're right, but if the blame for lazy programming can be shifted to the QA department, hell why not?

🌐
Python.org
discuss.python.org › ideas
Stop ignoring asserts when running in optimized mode - Ideas - Discussions on Python.org
January 13, 2022 - Following a Twitter discussion, I was interested to get some feedback around the idea of making a PEP to have assert statements always execute, independently of Python’s optimize mode. I will be taking some time in the near future to lay out what I think can be the pros and cons, but if some ...
Find elsewhere
🌐
GitHub
github.com › Technologicat › pyan › issues › 29
Asserts can be removed during bytecode optimization · Issue #29 · Technologicat/pyan
April 19, 2020 - From Codacy issues assert is removed with compiling to optimised byte code (python -o producing *.pyo files). This caused various protections to be removed. The use of assert is also considered as ...
Author   edumco
🌐
Python.org
discuss.python.org › ideas
Stop ignoring asserts when running in optimized mode - Page 3 - Ideas - Discussions on Python.org
January 19, 2022 - It may be useful to compare how C’s assert statement works. It is used in debug/testing phase for things that “should not happen”. (I like how @barry expressed it - “I never write assert s expecting them to ever be triggered”). One asserts that something is true - if it’s not then ...
🌐
GitHub
github.com › tirthajyoti › Optimization-Python
GitHub - tirthajyoti/Optimization-Python: General optimization (LP, MIP, QP, continuous and discrete optimization etc.) using Python · GitHub
General optimization (LP, MIP, QP, continuous and discrete optimization etc.) using Python - tirthajyoti/Optimization-Python
Starred by 232 users
Forked by 142 users
Languages   Jupyter Notebook
🌐
GitHub
github.com › samidhaVerma › optimization-test
GitHub - samidhaVerma/optimization-test: Test functions for optimization in Python.
Test functions for optimization in Python. Contribute to samidhaVerma/optimization-test development by creating an account on GitHub.
Author   samidhaVerma
🌐
GitHub
github.com › mmckerns › tutmom
GitHub - mmckerns/tutmom: Tutorial on "Modern Optimization Methods in Python" · GitHub
Tutorial on "Modern Optimization Methods in Python" - mmckerns/tutmom
Starred by 252 users
Forked by 118 users
Languages   Jupyter Notebook 99.8% | Python 0.2%
🌐
Reddit
reddit.com › r/python › why you should never use 'assert' in python
r/Python on Reddit: Why you should never use 'assert' in Python
November 9, 2018 - The -O switch removes assert statements, the -OO switch removes both assert statements and __doc__ strings. Since some programs may rely on having these available, you should only use this option if you know what you’re doing.
🌐
Sonar Community
community.sonarsource.com › rules and languages › new rules / language support
Feature: Python assert should be consider harmful - New rules / language support - Sonar Community
February 11, 2021 - In python the assert statements are ignored when running in optimize mode. This can lead to code misbehaving in production because the wrong check is in place. An old thread on the topic but still relevant: We would l…
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › statements › assert.html
assert — Python Reference (The Right Way) 0.1 documentation
These equivalences assume that __debug__ and AssertionError refer to the built-in variables with those names. In the current implementation, the built-in variable __debug__ is True under normal circumstances, False when optimization is requested (command line option -O).
🌐
pytest
docs.pytest.org › en › 7.1.x › announce › release-2.1.0.html
py.test 2.1.0: perfected assertions and bug fixes — pytest documentation
You can now safely use assert statements in test modules without having to worry about side effects or python optimization (“-OO”) options. This is achieved by rewriting assert statements in test modules upon import, using a PEP302 hook. See https://docs.pytest.org/en/stable/how-to/assert.html ...
🌐
GitHub
github.com › python › cpython › issues › 112909
compile(optimize=2) doesn't del docstrings/asserts, ignores -OO & PYTHONOPTIMIZE=2 · Issue #112909 · python/cpython
December 9, 2023 - import ast py_test_source = ''' def test() : """docstring""" assert True ... ''' ast_obj = compile( # https://docs.python.org/library/functions.html#compile source = py_test_source , filename = "" , mode = "exec" , flags = ast.PyCF_ONLY_AST , dont_inherit = 1 , optimize = 2 , ) print( ast.unparse( ast_obj ) )
Author   dreamflow
🌐
GitHub
github.com › gnthibault › Optimisation-Python
GitHub - gnthibault/Optimisation-Python: A set of notebooks related to convex optimization, variational inference and numerical methods for signal processing, machine learning, deep learning, graph analysis, bayesian programming, statistics or astronomy. · GitHub
This repository is intends to gather some basic optimisation algorithms in python, in order to study their behaviour and compare them in the a generic framework.
Starred by 31 users
Forked by 12 users
Languages   Jupyter Notebook