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
Join us and get access to thousands of tutorials and a community of expert Pythonistas. ... Unlock the full potential of your Python scripts in this hands-on lesson about the PYTHONOPTIMIZE environment variable. You’ll see, step by step, how a single environment variable switches the interpreter into optimized mode, turns off assertions, and trims bytecode for faster execution.
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
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
What does Python optimization (-O or PYTHONOPTIMIZE) do? - Stack Overflow
The docs only say that Python interpreter performs "basic optimizations", without going into any detail. Obviously, it's implementation dependent, but is there any way to get a feel for what type of More on stackoverflow.com
🌐 stackoverflow.com
🌐
SciPy
docs.scipy.org › doc › scipy › tutorial › optimize.html
Optimization (scipy.optimize) — SciPy v1.17.0 Manual
The solution is then updated \(\mathbf{x}_{k+1} = \mathbf{x}_{k} + \mathbf{p}\) and the trust-radius \(\Delta\) is adjusted according to the degree of agreement of the quadratic model with the real function. This family of methods is known as trust-region methods. The trust-ncg algorithm is a trust-region method that uses a conjugate gradient algorithm to solve the trust-region subproblem [NW]. ... >>> res = minimize(rosen, x0, method='trust-ncg', ... jac=rosen_der, hess=rosen_hess, ... options={'gtol': 1e-8, 'disp': True}) Optimization terminated successfully.
🌐
DataCamp
datacamp.com › tutorial › optimization-in-python
Optimization in Python: Techniques, Packages, and Best Practices | DataCamp
August 31, 2024 - Declarative syntax: CVXPY provides an intuitive way to model optimization problems using Python's mathematical expressions.
🌐
Python
wiki.python.org › moin › PythonSpeed › PerformanceTips
PythonSpeed/PerformanceTips - Python Wiki
The first step to speeding up your program is learning where the bottlenecks lie. It hardly makes sense to optimize code that is never executed or that already runs fast. I use two modules to help locate the hotspots in my code, profile and trace.
🌐
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 ...
Find elsewhere
🌐
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 …
🌐
GeeksforGeeks
geeksforgeeks.org › python › optimization-tips-python-code
Optimization Tips for Python Code - GeeksforGeeks
August 19, 2025 - When writing Python programs, performance often becomes important, especially when handling large datasets, running algorithms or working in production systems. This guide explains practical optimization techniques for Python.
🌐
Python
python-list.python.narkive.com › kAu3Sb4C › initializing-python-in-optimized-mode-from-c
Initializing Python in Optimized mode from C++
Post by JT When embedding Python in C++, is there anyway to initialize the interpreter so that it runs in optimized mode, equivalent to specifying the -O flag when running the interpreter from the command line? here's one way to do it: putenv("PYTHONOPTIMIZE=yes"); ...
🌐
Real Python
realpython.com › python-assert-statement
Python's assert: Debug and Test Your Code Like a Pro – Real Python
January 12, 2025 - Once you’ve debugged and tested ... production. You disable assertions by running Python in optimized mode with the -O or -OO options, or by setting the PYTHONOPTIMIZE environment variable. By the end of this tutorial, you’ll understand that:...
🌐
DEV Community
dev.to › blamsa0mine › python-performance-optimization-a-practical-detailed-guide-1mc7
Python Performance Optimization: A Practical, Detailed Guide - DEV Community
October 3, 2025 - A hands-on, copy–paste guide to measure, understand, and fix performance problems in Python. We’ll go from “it feels slow” to profiling → diffing → fixing → verifying—with runnable snippets and checklists you can reuse in every ...
🌐
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
🌐
Apmonitor
apmonitor.com › che263 › index.php › Main › PythonOptimization
Optimization with Python | Learn Programming
Optimization with Python - Problem-Solving Techniques for Chemical Engineers at Brigham Young University
🌐
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 - But for the slowest length method (a property) it is different. From this method the functions math.pow and math.sqrt are called adding the corresponding tottimes to the cumtime of length. In modern Python interpreters the math.pow function is applied to the buildin ** operator and the length method can be simplified to:
🌐
GitHub
github.com › PyDMD › PyDMD
GitHub - PyDMD/PyDMD: Python Dynamic Mode Decomposition · GitHub
You can read the winner tutorial (PDF format) in the tutorials folder. When citing PyDMD, please cite both of the following references: Demo, Tezzele, Rozza. PyDMD: Python Dynamic Mode Decomposition. Journal of Open Source Software, 2018.
Starred by 1.2K users
Forked by 358 users
Languages   Python 98.4% | TeX 1.6%
🌐
Stackify
stackify.com › how-to-optimize-python-code
Best Method of Python Code Optimization - Stackify - Ivanov
September 16, 2023 - Python code optimization is a way to make your program perform any task more efficiently and quickly with fewer lines of code, less memory, or other resources involved, while producing the right results.