python -O does the following currently:

  • completely ignores asserts
  • sets the special builtin name __debug__ to False (which by default is True)

and when called as python -OO

  • removes docstrings from the code

I don't know why everyone forgets to mention the __debug__ issue; perhaps it is because I'm the only one using it :) An if __debug__ construct creates no bytecode at all when running under -O, and I find that very useful.

Answer from tzot on Stack Overflow
🌐
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 ...
Discussions

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
Usage of assert statements in optimized mode
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... More on github.com
🌐 github.com
2
July 13, 2018
🌐
Real Python
realpython.com › lessons › pythonoptimize-variable
Setting the PYTHONOPTIMIZE Environment Variable (Video) – Real Python
03:03 You can use any integer number to set PYTHONOPTIMIZE, but Python only implements two levels of optimization. Using a number greater than 2 has no real effect on your compiled bytecode. 03:15 Setting PYTHONOPTIMIZE to 0 will cause the interpreter to return to normal mode.
Published   April 25, 2023
🌐
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 ...
🌐
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
Find elsewhere
🌐
GitHub
github.com › lark-parser › lark › issues › 185
Usage of assert statements in optimized mode · Issue #185 · lark-parser/lark
July 13, 2018 - Assert statements are ignored when Python is running in optimized mode. Optimize mode is used in some projects when Python is deployed and/or distributed.
Author   petermlm
🌐
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 - Optimized code is often harder to maintain and that should be avoided. However—sometimes the code is not fast enough and a speed-up is necessary. Then people often try to guess where to start to make the code faster. This is a bad idea and sometimes even experienced Python programmers are surprised about the real bottlenecks.
🌐
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 - The standard CPython interpreter has a handful of flags that can be set when executing a Python program, most of which are not directly supported by uv run. Do the uv maintainers have any plans to support additional flags, specifically the following optimization-related flags?
Author   gusutabopb
🌐
Python
wiki.python.org › moin › PythonSpeed › PerformanceTips
PythonSpeed/PerformanceTips - Python Wiki
Which method is appropriate will depend on what version of Python you're using and the characteristics of the data you are manipulating. Guido van Rossum wrote a much more detailed (and succinct) examination of loop optimization that is definitely worth reading.
🌐
Real Python
realpython.com › python-assert-statement
Python's assert: Debug and Test Your Code Like a Pro – Real Python
January 12, 2025 - This is the default or normal Python mode, in which all your assertions are enabled because __debug__ is True. On the other hand, if __debug__ is False, then the code under the outer if statement doesn’t run, meaning that your assertions will be disabled. In this case, Python is running in optimized mode.
🌐
Toucan
toucantoco.com › en › tech-blog › python-performance-optimization
Profiling and optimizing your Python code
Sure, Python is not compiled to optimized native code (yet) and therefore won’t rival C, Fortran or assembly in tightly nested loops. But most Python performance issues can be alleviated by improving the algorithm or using the right tool for the job.
🌐
Atleastfornow
atleastfornow.net › posts › py3-enable-optimisations
Compile-time Optimisations on Python 3 · atleastfornow.net
Firstly, you are doing the whole compile twice (first profiler-enabled, then optimised), as well as running the entirety of your representative code suite. On my computer the compile time (using make -j8) went from 1 minute to 24 minutes. This isn’t so bad if you’re compiling one Python to keep around for good, but when you’re compiling 38 variants on public CI, it can be unacceptable – which is why the Docker-official Python images don’t have it enabled.
🌐
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 - As anyone who has begun working in python will know, Scipy contains a number of useful packages covering a range of functions from statistics to linear algebra. Scipy has an Optimize package to help solve problems similar to what we faced in Treasury. The foundational math is linear algebra, and at its simplest, the functions included in Scipy Optimize are all essentially solvers designed to find the solution to a series of equations.
🌐
Reddit
reddit.com › r/python › how to optimize python code?
r/Python on Reddit: How to optimize python code?
January 28, 2022 -

Hi Pythonistas,

I'm interested in learning what optimization techniques you know for python code. I know its a general statement, but I'm interested in really pushing execution to the maximum.

I use the following -

  1. I declare __slots__ in custom classes

  2. I use typing blocks for typing imports

  3. I use builtins when possible

  4. I try to reduce function calls

  5. I use set lookups wherever possible

  6. I prefer iteration to recursion

Edit: I am using a profiler, and benchmarks. I'm working on a library - an ASGI Api framework. The code is async. Its not darascience. Its neither compatible with pypy, nor with numba..

What else?