当模块加载时,Python文档字符串和注释是否存储在内存中? [英] Are Python docstrings and comments stored in memory when module is loaded?

查看:267
本文介绍了当模块加载时,Python文档字符串和注释是否存储在内存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


当模块加载时,Python文档字符串和注释是否存储在内存中?

Are Python docstrings and comments stored in memory when module is loaded?

如果这是真的,因为我通常记录我的代码很好;这可能会影响内存使用吗?

I've wondered if this is true, because I usually document my code well; may this affect memory usage?

通常每个Python对象都有一个 __ doc __ 方法。这些文档字符串是从文件中读取还是以其他方式处理?

Usually every Python object has a __doc__ method. Are those docstrings read from the file, or processed otherwise?

我在论坛,Google和邮件列表中进行了搜索,找不到任何相关信息。

I've done searches here in the forums, Google and Mailing-Lists, but I haven't found any relevant information.

推荐答案

p>默认情况下,docstrings存在于 .pyc 字节码文件中,并从它们加载(注释不是)。如果使用 python -OO -OO 标志代表优化强烈,而不是 -O 这意味着优化),你得到并使用 .pyo 文件,而不是 .pyc 文件,并通过省略docstrings(除了由 -O 完成的优化, 语句)。例如,考虑具有以下内容的文件 foo.py

By default, docstrings are present in the .pyc bytecode file, and are loaded from them (comments are not). If you use python -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 remove assert statements). E.g., consider a file foo.py that has:

"""This is the documentation for my module foo."""

def bar(x):
  """This is the documentation for my function foo.bar."""
  return x + 1

shell session ...:

you could have the following shell session...:

$ python -c'import foo; print foo.bar(22); print foo.__doc__'
23
This is the documentation for my module foo.
$ ls -l foo.pyc
-rw-r--r--  1 aleax  eng  327 Dec 30 16:17 foo.pyc
$ python -O -c'import foo; print foo.bar(22); print foo.__doc__'
23
This is the documentation for my module foo.
$ ls -l foo.pyo
-rw-r--r--  1 aleax  eng  327 Dec 30 16:17 foo.pyo
$ python -OO -c'import foo; print foo.bar(22); print foo.__doc__'
23
This is the documentation for my module foo.
$ ls -l foo.pyo
-rw-r--r--  1 aleax  eng  327 Dec 30 16:17 foo.pyo
$ rm foo.pyo
$ python -OO -c'import foo; print foo.bar(22); print foo.__doc__'
23
None
$ ls -l foo.pyo
-rw-r--r--  1 aleax  eng  204 Dec 30 16:17 foo.pyo

请注意,由于我们使用 -O 首先, .pyo 文件是327字节 - 即使使用 -OO 后,因为 .pyo 文件仍然存在,Python没有重建/覆盖它,它只是使用现有的。移除现有的 .pyo (或等效地, touch foo.py ,让Python知道 .pyo 是过时的)意味着Python重建它(在这种情况下,在磁盘上保存123个字节,并且在模块导入时多一点 - 但都是 .__ doc __ 条目消失并替换为)。

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 by None).

这篇关于当模块加载时,Python文档字符串和注释是否存储在内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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