“-O”有什么用?运行Python的标志?
Python 可以在优化模式下运行脚本 (python - O
) 关闭调试,删除 assert
语句,IIRC 它还删除了文档字符串。
不过,我还没有看到它被使用过。 python -O 是否实际使用过?如果是这样,那又是为了什么?
Python can run scripts in optimized mode (python -O
) which turns off debugs, removes assert
statements, and IIRC it also removes docstrings.
However, I have not seen it used. Is python -O
actually used? If so, what for?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
python -O 当前执行以下操作:
__debug__
设置为 False (默认情况下为 True),并且当调用为 python -OO 时从
我不知道的 为什么每个人都忘记提及 __debug__ 问题;也许是因为我是唯一使用它的人:)
if __debug__
构造在-O
下运行时根本不创建任何字节码,我发现这非常有用。python -O does the following currently:
__debug__
to False (which by default is True)and when called as python -OO
I don't know why everyone forgets to mention the
__debug__
issue; perhaps it is because I'm the only one using it :) Anif __debug__
construct creates no bytecode at all when running under-O
, and I find that very useful.如果您分发仅包含
.pyo
文件的任何存档表单,它可以节省少量内存和磁盘空间。 (如果您经常使用assert
,并且可能在复杂的条件下,节省的时间可能不小,并且还可以延长运行时间)。所以,它绝对不是无用——当然它正在被使用(如果你将一个Python编码的服务器程序部署到大量N台服务器机器上,为什么曾经会你想浪费 N * X 字节来保留没有人能够访问的文档字符串?!)。当然,如果能节省更多就更好了,但是,嘿——不要浪费,不要!-)
所以保留这个功能几乎是理所当然的(无论如何,提供起来都很简单,你知道) ;-) 在 Python 3 中——为什么还要添加“epsilon”来增加后者的采用难度?-)
It saves a small amount of memory, and a small amount of disk space if you distribute any archive form containing only the
.pyo
files. (If you useassert
a lot, and perhaps with complicated conditions, the savings can be not trivial and can extend to running time too).So, it's definitely not useless -- and of course it's being used (if you deploy a Python-coded server program to a huge number N of server machines, why ever would you want to waste N * X bytes to keep docstrings which nobody, ever, would anyway be able to access?!). Of course it would be better if it saved even more, but, hey -- waste not, want not!-)
So it's pretty much a no-brainer to keep this functionality (which is in any case trivially simple to provide, you know;-) in Python 3 -- why add even "epsilon" to the latter's adoption difficulties?-)
不同 Linux 发行版中的预打包软件通常使用 -O 进行字节编译。例如,以下内容来自 Python 应用程序的 Fedora 打包指南:
Prepacked software in different Linux distributions often comes byte-compiled with -O. For example, this if from Fedora packaging guidelines for python applications:
删除断言意味着小性能提升,因此您可以将其用于“发布”代码。
因此,只要此模式下没有任何真正的优化,您就可以忽略它。
Removing assertions means a small performance benefit, so you could use this for "release" code.
So, as long as there isn't any real optimization in this mode, you can ignore it.