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
🌐
Real Python
realpython.com › lessons › pythonoptimize-variable
Setting the PYTHONOPTIMIZE Environment Variable (Video) – Real Python
You can also run Python in optimized ... appropriate value. 00:13 For example, setting this variable to a non-empty string is equivalent to running Python with the previously seen -O option....
Published   April 25, 2023
Discussions

Does anyone use `-O` or `-OO` or `PYTHONOPTIMIZE=1` in their deployments? - Ideas - Discussions on Python.org
I expect the answer to the question in the subject for most is “No”… In that case, you are not who I’m interested in hearing from. It’d be good to hear from CPython users who actually use -O, -OO, or PYTHONOPTIMIZE=1 to have Python elide assert statements (performance?) or (-OO) omit ... More on discuss.python.org
🌐 discuss.python.org
12
November 1, 2023
Stop ignoring asserts when running in optimized mode - Ideas - Discussions on Python.org
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 ... More on discuss.python.org
🌐 discuss.python.org
6
January 13, 2022
Add support for optimized mode (`-O`) in `uv run`
Do the uv maintainers have any ... optimization-related flags? -O : remove assert and __debug__-dependent statements; add .opt-1 before .pyc extension; also PYTHONOPTIMIZE=x -OO : do -O changes and also discard docstrings; add .opt-2 before .pyc extension · To be fair, I don't think it's absolutely necessary for this to be supported since the workaround (see examples below) is ... More on github.com
🌐 github.com
6
February 16, 2025
optimization - What are the implications of running python with the optimize flag? - Stack Overflow
What does Python do differently when running with the -O (optimize) flag? More on stackoverflow.com
🌐 stackoverflow.com
May 14, 2010
🌐
Python.org
discuss.python.org › ideas
Does anyone use `-O` or `-OO` or `PYTHONOPTIMIZE=1` in their deployments? - Ideas - Discussions on Python.org
November 1, 2023 - I expect the answer to the question in the subject for most is “No”… In that case, you are not who I’m interested in hearing from. It’d be good to hear from CPython users who actually use -O, -OO, or PYTHONOPTIMIZE=1 to have Python elide assert statements (performance?) or (-OO) omit ...
🌐
Python
wiki.python.org › moin › PythonSpeed › PerformanceTips
PythonSpeed/PerformanceTips - Python Wiki
For example: xrange(sys.maxint) # No loop, and no call to .next, so no numbers are instantiated · And for this reason, the code runs instantly. If you substitute range there, Python will lock up; it will be too busy allocating sys.maxint number objects (about 2.1 billion on the typical PC) to do anything else. Eventually, it will run out of memory and exit. In Python versions before 2.2, xrange objects also supported optimizations such as fast membership testing (i in xrange(n)).
🌐
SciPy
docs.scipy.org › doc › scipy › tutorial › optimize.html
Optimization (scipy.optimize) — SciPy v1.17.0 Manual
The following example considers the single-variable transcendental equation ... >>> import numpy as np >>> from scipy.optimize import root >>> def func(x): ...
🌐
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
🌐
GeeksforGeeks
geeksforgeeks.org › python › optimization-tips-python-code
Optimization Tips for Python Code - GeeksforGeeks
August 19, 2025 - n = [1, 2, 3, 4, 5] # Inefficient: ... list comprehension s = [num ** 2 for num in n] print("Optimized:", s) ... First method uses explicit looping with append() while the second is more concise and efficient. Using a list comprehension reduces overhead, resulting in faster execution and cleaner code. Local variables are faster because Python doesn’t need to search the global scope each time. Storing frequently used objects or functions locally improves speed and keeps code cleaner. Example: It shows how storing a method in a local variable makes repeated calls faster than accessing it from object each time...
🌐
Real Python
realpython.com › python-assert-statement
Python's assert: Debug and Test Your Code Like a Pro – Real Python
January 12, 2025 - For an example, open your command line or terminal within the directory containing the circle.py file and run an interactive session with the python -O command. Once there, run the following code: ... >>> # Running Python in optimized mode >>> ...
🌐
GitHub
github.com › astral-sh › uv › issues › 11547
Add support for optimized mode (`-O`) in `uv run` · Issue #11547 · astral-sh/uv
February 16, 2025 - Do the uv maintainers have any ... optimization-related flags? -O : remove assert and __debug__-dependent statements; add .opt-1 before .pyc extension; also PYTHONOPTIMIZE=x -OO : do -O changes and also discard docstrings; add .opt-2 before .pyc extension · To be fair, I don't think it's absolutely necessary for this to be supported since the workaround (see examples below) is ...
Author   gusutabopb
🌐
Medium
medium.com › @bremer_21076 › how-to-optimize-python-code-the-right-way-a-beginners-guide-to-profiling-34c4c0f07dc4
How to optimize Python code the right way — a beginners guide to profiling | by Klaus Bremer | Medium
November 28, 2023 - Vector class before optimization: 49.4 ms ± 305 µs Vector class after optimization : 44.6 ms ± 144 µs · For this simple example an experienced Python programmer would be able to identify the bottleneck at first sight, without using a profiler. However, real world applications are more complex and bottlenecks are often hidden deep in the code.
🌐
GitHub
github.com › python › typing › discussions › 1598
Type hints and optimized mode · python/typing · Discussion #1598
While reviewing a PR I came across a somewhat interesting case - the validity of the return type depending on whether Python is running in optimized mode (starting Python with -O or PYTHONOPTIMIZE ...
Author   python
🌐
Python
python-list.python.narkive.com › kAu3Sb4C › initializing-python-in-optimized-mode-from-c
Initializing Python in Optimized mode from C++
Thanks, John set Py_OptimizeFlag to 1 for -O, and 2 for -OO. Do this prior to calling Py_Initialize(); You can also improve your startup time by setting Py_NoSiteFlag = 1...assuming you don't need to load site.py for example: extern int Py_OptimizeFlag; extern int Py_NoSiteFlag; ...
🌐
GitHub
github.com › lark-parser › lark › issues › 185
Usage of assert statements in optimized mode · Issue #185 · lark-parser/lark
July 13, 2018 - I've noticed that Lark uses assert statements in some places outside of tests. Just by doing git grep assert in the lark directory we can see a big list of these. Assert statements are ignored when Python is running in optimized mode. Op...
Author   petermlm
🌐
Toucan
toucantoco.com › en › tech-blog › python-performance-optimization
Profiling and optimizing your Python code
Numba is an effort to speed up Python code through the use of JIT (just in time) compilation and even GPU hardware.
🌐
Medium
medium.com › @jeffmarvel › a-brief-exploration-of-optimization-in-python-4877d48b2420
A Brief Exploration of Optimization in Python | by Jeff Marvel | Medium
September 27, 2021 - What outcome (objective function) do you want to optimize? What is your data of interest and how do they contribute to the objective function? How much will you allow your data to increase or decrease? And what constraints do you need to observe? Unsurprisingly, Python, the language of Data Science, has a number of existing libraries to help answer these questions.
🌐
AppSignal
blog.appsignal.com › 2025 › 05 › 28 › ways-to-optimize-your-code-in-python.html
Ways to Optimize Your Code in Python | AppSignal Blog
May 28, 2025 - Optimized algorithms: Experienced developers highly optimize standard library functions to perform common tasks efficiently. Reduced overhead: Invoking a function implemented in C avoids the overhead associated with Python's dynamic typing and interpreted execution. Suppose we sort a list with a lot of numbers. The following example compares performance when creating a custom function versus using a built-in one: