擅长:python、mysql、java
<p>默认情况下,Python docstring是无限期保留的,因为它们可以通过函数或模块的\uuu doc_uu属性访问。例如,在测试.py公司名称:</p>
<pre><code>"""This is a test module."""
def f():
"""This is a test function."""
pass
</code></pre>
<p>然后:</p>
^{pr2}$
<p>解释器的<code>-OO</code>选项显然会导致它从生成的<code>.pyo</code>文件中删除docstring,但它没有我预期的效果:</p>
<pre><code>$ 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.'
>>>
</code></pre>
<p>事实上,使用<code>-OO</code>生成的<code>test.pyo</code>文件与没有命令行参数生成的<code>test.pyc</code>文件完全相同。有人能解释这种行为吗?在</p>