如何使用pydoc在Python文件中生成方法和属性列表

如何使用pydoc在Python文件中生成方法和属性列表,python,introspection,pydoc,Python,Introspection,Pydoc,我编写了一些类,在其中一些类中,我添加了一些文档字符串,比如在类标题中 现在我想使用pydoc生成文档,但我意识到pydoc不会打印任何东西,除非我在类中实际编写文档部分,这不是我想要的 有没有办法让pydoc生成所有属性、方法及其类型的列表,包括所需参数的类型(如果有)和返回的类型(如果有) 如果我有这样一门课: class myclass(object): def __init__(anumber=2, astring="hello"): self.a = anum

我编写了一些类,在其中一些类中,我添加了一些文档字符串,比如在类标题中

现在我想使用pydoc生成文档,但我意识到pydoc不会打印任何东西,除非我在类中实际编写文档部分,这不是我想要的

有没有办法让pydoc生成所有属性、方法及其类型的列表,包括所需参数的类型(如果有)和返回的类型(如果有)

如果我有这样一门课:

class myclass(object):

    def __init__(anumber=2, astring="hello"):
        self.a = anumber
        self.b = astring

    def printme(self):
        thestring = self.a + self.b + "\nthat's all folks\n"
        return thestring

    def setvalues(self, a_number, a_string):
        self.a = a_number
        self.b = a_string
我想打印一些内容,包括类名、类方法、数据类型:

class name
what parameters it takes in the init and the type of the parameters

method name
what parameters it takes and the type of the parameters
what value return and its type.
我相信pydoc不会这么做的。还有其他办法吗


我可以添加文档字符串以供稍后解释,但首先,我想打印出模块中的内容,以了解它需要什么、返回什么等等。

Pydoc应该向您提供框架详细信息,与
帮助(myclass)
相同,这将向您显示没有任何文档字符串的类的函数签名。您可以使用pydoc模块获取以下帮助信息:

$ pydoc MyClass.myclass
Help on class myclass in MyClass:

class myclass(builtins.object)
 |  Methods defined here:
 |  
 |  __init__(anumber=2, astring='hello')
 |  
 |  printme(self)
 |  
 |  setvalues(self, a_number, a_string)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
您可以使用
pydoc
模块以编程方式执行此操作:

>>> import pydoc
>>> import MyClass
>>> h = pydoc.plain(pydoc.render_doc(MyClass))
>>> print h