是否可以在脚本中设置 python -O(优化)标志? [英] Is it possible to set the python -O (optimize) flag within a script?
问题描述
我想根据脚本的命令行参数在 python 脚本中的 runtime 中设置优化标志 (python -O myscript.py
)myscript.py --optimize
或 myscript --no-debug
.我想跳过 assert
语句而不将它们全部删除.或者有没有更好的方法来有效地忽略部分 python 代码.在 C++ 中,#if
和 #ifdef
是否有 Python 等价物?
I'd like to set the optimize flag (python -O myscript.py
) at runtime within a python script based on a command line argument to the script like myscript.py --optimize
or myscript --no-debug
. I'd like to skip assert
statements without iffing all of them away. Or is there a better way to efficiently ignore sections of python code. Are there python equivalents for #if
and #ifdef
in C++?
推荐答案
-O
是一个编译器标志,你不能在运行时设置它,因为那时脚本已经编译过了.
-O
is a compiler flag, you can't set it at runtime because the script already has been compiled by then.
Python 没有任何东西可以与 #if
这样的编译器宏相媲美.
Python has nothing comparable to compiler macros like #if
.
只需编写一个 start_my_project.sh
脚本来设置这些标志.
Simply write a start_my_project.sh
script that sets these flags.
这篇关于是否可以在脚本中设置 python -O(优化)标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!