加载模块时,Python 文档字符串和注释是否存储在内存中?
加载模块时,Python 文档字符串和注释是否存储在内存中?
我想知道这是否属实,因为我通常会很好地记录我的代码;这会影响内存使用吗?
通常每个Python对象都有一个__doc__
方法。这些文档字符串是从文件中读取的,还是以其他方式处理的?
我已经在论坛、Google 和邮件列表中进行了搜索,但没有找到任何相关信息。
你知道更多吗?
Are Python docstrings and comments stored in memory when a module is loaded?
I've wondered if this is true, because I usually document my code well; may this affect memory usage?
Usually every Python object has a __doc__
method. Are those docstrings read from the file, or processed otherwise?
I've done searches here in the forums, Google and Mailing-Lists, but I haven't found any relevant information.
Do you know better?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
默认情况下,文档字符串存在于
.pyc
字节码文件中,并从中加载(注释则不然)。如果您使用 python -OO(-OO
标志代表“强烈优化”,而不是代表“温和优化”的-O
) ),您获取并使用.pyo
文件而不是.pyc
文件,并且这些文件通过省略文档字符串进行优化(除了-O 完成的优化之外)
,它删除了assert
语句)。例如,考虑一个文件foo.py
,它具有:您可以有以下 shell 会话...:
请注意,由于我们首先使用
-O
,.pyo
文件为 327 字节——即使在使用-OO
之后也是如此,因为.pyo
文件是 327 字节。 文件仍然存在,Python 没有重建/覆盖它,它只是使用现有的.pyo
(或者等效地,touch foo.py<。 /code> 以便 Python 知道
.pyo
已“过时”)意味着 Python 会重建它(在本例中,在磁盘上节省 123 个字节,当模块的已导入 - 但所有.__doc__
条目都会消失并被None
替换)。By default, docstrings are present in the
.pyc
bytecode file, and are loaded from them (comments are not). If you usepython -OO
(the-OO
flag stands for "optimize intensely", as opposed to-O
which stands for "optimize mildly), you get and use.pyo
files instead of.pyc
files, and those are optimized by omitting the docstrings (in addition to the optimizations done by-O
, which removeassert
statements). E.g., consider a filefoo.py
that has:you could have the following shell session...:
Note that, since we used
-O
first, the.pyo
file was 327 bytes -- even after using-OO
, because the.pyo
file was still around and Python didn't rebuild/overwrite it, it just used the existing one. Removing the existing.pyo
(or, equivalently,touch foo.py
so that Python knows the.pyo
is "out of date") means that Python rebuilds it (and, in this case, saves 123 bytes on disk, and a little bit more when the module's imported -- but all.__doc__
entries disappear and are replaced byNone
).是的,文档字符串是从文件中读取的,但这不应阻止您编写它们。永远不要为了性能而牺牲代码的可读性,除非您完成了性能测试并发现您担心的事情实际上是程序中导致问题的瓶颈。我认为文档字符串在任何现实情况下都不太可能造成任何可衡量的性能影响。
Yes the docstrings are read from the file, but that shouldn't stop you writing them. Never ever compromise readability of code for performance until you have done a performance test and found that the thing you are worried about is in fact the bottleneck in your program that is causing a problem. I would think that it is extremely unlikely that a docstring will cause any measurable performance impact in any real world situation.
它们从文件中读取(当文件编译为 pyc 或加载 pyc 时——它们必须在
object.__doc__
下可用)但是 否 -->在任何合理的情况下,这都不会显着影响性能,或者您真的在编写多兆字节的文档字符串吗?They are getting read from the file (when the file is compiled to pyc or when the pyc is loaded -- they must be available under
object.__doc__
) but no --> this will not significantly impact performance under any reasonable circumstances, or are you really writing multi-megabyte doc-strings?文档字符串被编译到 .pyc 文件中,并加载到内存中。注释在编译期间被丢弃,除了在编译期间忽略它们所花费的微不足道的额外时间外,对任何事情都没有影响(这种情况仅在对 .py 文件进行任何更改后发生一次,除了主脚本,每次修改时都会重新编译)跑步)。
另请注意,仅当这些字符串是模块、类定义或函数定义中的第一个时,它们才会被保留。您几乎可以在任何地方包含附加字符串,但它们将在编译期间像注释一样被丢弃。
Docstrings are compiled into the .pyc file, and are loaded into memory. Comments are discarded during compilation and have no impact on anything except the insignificant extra time taken to ignore them during compilation (which happens once only after any change to a .py file, except for the main script which is re-compiled every time it is run).
Also note that these strings are preserved only if they are the first thing in the module, class definition, or function definition. You can include additional strings pretty much anywhere, but they will be discarded during compilation just as comments are.
正如其他答案提到的,注释在编译过程中被丢弃,但文档字符串存储在 .pyc 文件中并加载到内存中。
在 .pyc 文件中,存在使用 marshal 序列化的代码对象。虽然它不应该是可读的,但你仍然可以找到一些东西。那么为什么不直接看看它确实在 .pyc 文件中呢?
输出:
函数的代码对象中在哪里引用了它?在 .co_consts
输出中:
As other answers mentioned, comments are discarded in compilation process but docstrings are stored in .pyc file and are loaded into the memory.
In .pyc files, there are code objects that are serialized with marshal. Although it's not supposed to be readable but you can still find something. So why not just see that it is indeed in .pyc file?
output:
where is it referenced in the function's code object? in
.co_consts
output: