I regularly use -O (strip debug code) for production application code to gain some extra performance and -OO (+= strip doc strings) for cases where I additionally need to reduce the shipped bytecode file sizes.
E.g. the compressed eGenix PyRun size goes down by some 8% using -OO last time I measured this. For PyRun, I also intend to use PYTHONNODEBUGRANGES with Python 3.11+ once I have it ported to 3.11 and later.
With -O you have to be careful with some code, since there are still Python programmers who use assert
where they should be using if
-statements.
Using -OO works well, except for some packages which use doc-strings to keep extra information. Some older parser generators had the grammar bits in method doc-strings. A few CLI tools use the module doc-string for printing out a help screen. Of course, interactive help also doesn’t work, but for production apps, this is hardly ever needed.
Both cases can be addressed, of course, by simply not using the options.
BTW: It would be useful to have more flexibility with these options to be able to switch off certain things individually, e.g.
-O=debug,docs,errors
With debug
switching off debug statements, docs
removing doc-strings, errors
removing detailed line error information, etc.
I’m sure the upcoming JIT will provide more ways to optimize things, so the list could be extended.