是否可以在脚本中设置python -O(优化)标志? [英] Is it possible to set the python -O (optimize) flag within a script?
问题描述
我想基于myscript.py --optimize
或myscript --no-debug
等脚本的命令行参数,在python脚本中的运行时上设置优化标志(python -O myscript.py
).我想跳过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屋!