“-O”有什么用?运行Python的标志?

发布于 2024-08-18 01:36:30 字数 362 浏览 9 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

没有心的人 2024-08-25 01:36:30

python -O 当前执行以下操作:

  • 完全忽略断言
  • 将特殊内置名称 __debug__ 设置为 False (默认情况下为 True)

,并且当调用为 python -OO 时从

  • 代码中删除文档字符串

我不知道的 为什么每个人都忘记提及 __debug__ 问题;也许是因为我是唯一使用它的人:) if __debug__ 构造在 -O 下运行时根本不创建任何字节码,我发现这非常有用。

python -O does the following currently:

  • completely ignores asserts
  • sets the special builtin name __debug__ to False (which by default is True)

and when called as python -OO

  • removes docstrings from the code

I don't know why everyone forgets to mention the __debug__ issue; perhaps it is because I'm the only one using it :) An if __debug__ construct creates no bytecode at all when running under -O, and I find that very useful.

祁梦 2024-08-25 01:36:30

如果您分发仅包含 .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 use assert 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?-)

心奴独伤 2024-08-25 01:36:30

不同 Linux 发行版中的预打包软件通常使用 -O 进行字节编译。例如,以下内容来自 Python 应用程序的 Fedora 打包指南

过去,通常的做法是对 .pyo 文件进行 %ghost,以节省用户文件系统上的少量空间。然而,这有两个问题: 1. 使用 SELinux,如果用户运行 python -O [APP],当 .pyos 不存在时,它将尝试写入它们。这会导致日志中出现 AVC 拒绝记录。 2. 如果系统管理员运行 python -OO [APP],将创建不带文档字符串的 .pyos。有些程序需要文档字符串才能运行。在随后使用 python -O [APP] 运行时,即使请求了不同的优化级别,python 也将使用缓存的 .pyos。解决此问题的唯一方法是找出 .pyos 所在位置并将其删除。

当前处理 pyo 文件的方法是按原样包含它们,没有 %ghosting。

Prepacked software in different Linux distributions often comes byte-compiled with -O. For example, this if from Fedora packaging guidelines for python applications:

In the past it was common practice to %ghost .pyo files in order to save a small amount of space on the users filesystem. However, this has two issues: 1. With SELinux, if a user is running python -O [APP] it will try to write the .pyos when they don't exist. This leads to AVC denial records in the logs. 2. If the system administrator runs python -OO [APP] the .pyos will get created with no docstrings. Some programs require docstrings in order to function. On subsequent runs with python -O [APP] python will use the cached .pyos even though a different optimization level has been requested. The only way to fix this is to find out where the .pyos are and delete them.

The current method of dealing with pyo files is to include them as is, no %ghosting.

梦情居士 2024-08-25 01:36:30

删除断言意味着性能提升,因此您可以将其用于“发布”代码。

因此,只要此模式下没有任何真正的优化,您就可以忽略它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文