是否可以在脚本中设置python -O(优化)标志? [英] Is it possible to set the python -O (optimize) flag within a script?

查看:105
本文介绍了是否可以在脚本中设置python -O(优化)标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于myscript.py --optimizemyscript --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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆