我可以打印 object.__doc__ 但我无法将其保存到文件中

I can print object.__doc__ but I can't save it to a file

如给定对象(模块或class)的主题行所述

print (object.__doc__)   works
document.write (object.__doc__)
TypeError" write: () argument must be a str not None

我确实得到 type(object.__doc__) 是 None

更新: Grrr...确实列表中的第一个对象没有文档字符串 现在错误变成了 TypeError" 必须是 str 而不是类型

假设你说的 object 不是内置的 object class 而是你自己的 class.

要使 __doc__ 不是 None,所讨论的 class 必须具有文档字符串。

对比度:

>>> class X(object):
...    pass
... 
>>> type(X.__doc__)
<type 'NoneType'>

与:

>>> class Y(object):
...    "Class Y"
... 
>>> type(Y.__doc__)
<type 'str'>