What is the use of Python's basic optimizations mode? (python -O)

后端 未结 7 745
终归单人心
终归单人心 2020-12-25 10:24

Python has a flag -O that you can execute the interpreter with. The option will generate \"optimized\" bytecode (written to .pyo files), and given twice, it wil

7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-25 10:28

    On stripping assert statements: this is a standard option in the C world, where many people believe part of the definition of ASSERT is that it doesn't run in production code. Whether stripping them out or not makes a difference depends less on how many asserts there are than on how much work those asserts do:

    def foo(x):
        assert x in huge_global_computation_to_check_all_possible_x_values()
        # ok, go ahead and use x...
    

    Most asserts are not like that, of course, but it's important to remember that you can do stuff like that.

    As for stripping docstrings, it does seem like a quaint holdover from a simpler time, though I guess there are memory-constrained environments where it could make a difference.

提交回复
热议问题