Python - 大文档字符串会浪费内存吗?

发布于 2024-08-30 09:26:12 字数 291 浏览 8 评论 0原文

我知道在Python中,字符串只是一个表达式,并且在将控制权返回给代码的调用者时,字符串本身将立即被垃圾收集,但是......

  1. 大型类/方法文档字符串 你的代码:它们浪费内存吗 通过构建字符串对象?
  2. 模块级文档字符串:它们是吗 由解释器无限存储?

这还重要吗?我唯一担心的是,如果我使用像 Django 这样的大型框架或多个大型开源库,它们往往会被很好地记录下来,可能有数兆字节的文本。在这些情况下,文档字符串是加载到内存中供一路使用的代码使用,然后保留在那里,还是像普通字符串一样立即收集?

I understand that in Python a string is simply an expression and a string by itself would be garbage collected immediately upon return of control to a code's caller, but...

  1. Large class/method doc strings in
    your code: do they waste memory
    by building the string objects up?
  2. Module level doc strings: are they
    stored infinitely by the interpreter?

Does this even matter? My only concern came from the idea that if I'm using a large framework like Django, or multiple large open source libraries, they tend to be very well documented with potentially multiple megabytes of text. In these cases are the doc strings loaded into memory for code that's used along the way, and then kept there, or is it collected immediately like normal strings?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

落花浅忆 2024-09-06 09:26:12
  • “我理解,在Python中,字符串只是一个表达式,并且在将控制权返回给代码的调用者时,字符串本身将立即被垃圾收集”,我认为这表明了一种误解。文档字符串被评估一次(不是在每次函数调用时),并且至少与函数一样长地保持活动状态。

  • “这还重要吗?”当谈到优化时,不能通过抽象思考来回答,而是通过测量来回答。在内存密集型应用程序中,“数兆字节”的文本可能并不是很多。节省内存的解决方案可能存在于其他地方,您可以通过测量来确定情况是否如此。

  • Python 的 -OO 命令行开关删除文档字符串。

  • "I understand that in Python a string is simply an expression and a string by itself would be garbage collected immediately upon return of control to a code's caller" indicates a misunderstanding, I think. A docstring is evaluated once (not on every function call) and stays alive at least as long as the function does.

  • "Does this even matter?" when it comes to optimization is not answered by thinking about it abstractly but by measuring. "Multiple megabytes" of text isn't probably isn't a lot in a memory-intensive application. The solution for saving memory likely lives elsewhere and you can determine whether that is the case by measurement.

  • Python's -OO command line switch removes docstrings.

千笙结 2024-09-06 09:26:12

默认情况下,Python 文档字符串会无限期保留,因为它们可以通过函数或模块的 __doc__ 属性进行访问。例如,在 test.py 中使用以下内容:

"""This is a test module."""

def f():
   """This is a test function."""
   pass

然后:

$ python
Python 2.5.1 (r251:54863, Oct 30 2007, 13:54:11) 
[GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> test.__doc__
'This is a test module.'
>>> test.f.__doc__
'This is a test function.'
>>> 

解释器的 -OO 选项显然会导致它从生成的 .pyo 文件中删除文档字符串,但它不会没有达到我期望的效果:

$ python -OO
Python 2.5.1 (r251:54863, Oct 30 2007, 13:54:11) 
[GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> test.__file__
'/tmp/test.py'
>>> 
$ grep "This is a test" /tmp/test.pyo
Binary file /tmp/test.pyo matches
$ python -OO
Python 2.5.1 (r251:54863, Oct 30 2007, 13:54:11) 
[GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> test.__file__
'/tmp/test.pyo'
>>> test.__doc__
'This is a test module.'
>>> 

事实上,使用 -OO 生成的 test.pyo 文件与 test.pyc 相同不使用命令行参数生成的文件。谁能解释这种行为?

Python docstrings by default are kept around indefinitely, since they're accessible via the __doc__ attribute of a function or a module. For example, with the following in test.py:

"""This is a test module."""

def f():
   """This is a test function."""
   pass

Then:

$ python
Python 2.5.1 (r251:54863, Oct 30 2007, 13:54:11) 
[GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> test.__doc__
'This is a test module.'
>>> test.f.__doc__
'This is a test function.'
>>> 

The -OO option to the interpreter apparently causes it to remove docstrings from the generated .pyo files, but it doesn't have the effect I would expect:

$ python -OO
Python 2.5.1 (r251:54863, Oct 30 2007, 13:54:11) 
[GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> test.__file__
'/tmp/test.py'
>>> 
$ grep "This is a test" /tmp/test.pyo
Binary file /tmp/test.pyo matches
$ python -OO
Python 2.5.1 (r251:54863, Oct 30 2007, 13:54:11) 
[GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> test.__file__
'/tmp/test.pyo'
>>> test.__doc__
'This is a test module.'
>>> 

And in fact, the test.pyo file generated with -OO is identical to the test.pyc file generated with no command-line arguments. Can anyone explain this behavior?

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