Python docstringは、関数またはモジュールの__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
オプションにより、生成されたファイルからdocstringが削除されるよう.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.'
>>>
実際、でtest.pyo
生成されたファイルは、コマンドライン引数なしで生成さ-OO
れたファイルと同じです。test.pyc
誰かがこの振る舞いを説明できますか?