默认情况下,Python docstrings被无限期地保留下来,因为它们可以通过函数或模块的__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
文件identi cal到不带命令行参数生成的test.pyc
文件。谁能解释这种行为?
相关:http://stackoverflow.com/questions/2731022/do-comments-slow-down-an-interpreted-language – 2010-04-29 16:17:30
可能的重复http://stackoverflow.com/questions/1983177/do-python- docstrings-and-comments-are-stored-in-memory-when-module-is-loaded – 2010-04-29 16:17:57