- Secret Recipes of the Python Ninja
- Cody Jackson
- 155字
- 2025-04-04 16:56:19
How to do it...
- To create bytecode, simply execute a Python program via python <program>.py.
- When running a Python command from the command line, there are a couple of switches that can reduce the size of the compiled bytecode. Be aware that some programs may expect the statements that are removed from the following examples to function correctly, so only use them if you know what to expect.
-O removes assert statements from the compiled code. These statements provide some debugging help when testing the program, but generally aren't required for production code.
-OO removes both assert and __doc__ strings for even more size reduction.
- Loading programs from bytecode into memory is faster than with source code, but actual program execution is no faster (due to the nature of the Python interpreter).
- The compileall module can generate bytecode for all modules within a directory. More information on the command can be found at https://docs.python.org/3.6/library/compileall.html.