有没有办法让我在Python类上获得详细的格式化信息?

时间:2010-02-08 22:43:02

标签: python

所以,我知道我可以使用dir()获取有关类成员的信息等。我正在寻找的是一种获取格式良好的报告的方法,该报告与类相关的所有内容(成员,docstrings,继承层次结构)等等。)。

我希望能够在命令行上运行它,以便我可以更好地探索代码和调试。

6 个答案:

答案 0 :(得分:5)

尝试在课堂上致电help

答案 1 :(得分:4)

从命令行尝试:

pydoc modulename

答案 2 :(得分:1)

尝试解释器中内置的help()工具。 E.g。

class X(object):
    """Docstring for an example class."""
    def __init__(self):
        """Docstring for X.__init__()."""
        pass
    def method1(self, x):
        """Docstring for method1()."""
        print x

>>> help(X)
Help on class X in module __main__:

class X(__builtin__.object)
 |  Docstring for an example class.
 |  
 |  Methods defined here:
 |  
 |  __init__(self)
 |      Docstring for X.__init__().
 |  
 |  method1(self, x)
 |      Docstring for method1().
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __dict__ = <dictproxy object>
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__ = <attribute '__weakref__' of 'X' objects>
 |      list of weak references to the object (if defined)

这适用于任何事情,例如

>>> help(dir):
Help on built-in function dir in module __builtin__:

dir(...)
    dir([object]) -> list of strings

    Return an alphabetized list of names comprising (some of) the attributes
    of the given object, and of attributes reachable from it:

    No argument:  the names in the current scope.
    Module object:  the module attributes.
    Type or class object:  its attributes, and recursively the attributes of
        its bases.
    Otherwise:  its attributes, its class's attributes, and recursively the
        attributes of its class's base classes.

答案 3 :(得分:0)

不确定你需要什么,但Mark Pilgrim的“Dive into Python”中的this chapter可能有效。

答案 4 :(得分:0)

尝试ipython

答案 5 :(得分:0)

你想要内省。这篇文章很好:http://www.ibm.com/developerworks/library/l-pyint.html

另一方面,对于调试,您可能只想将Eclipse与Python插件一起使用。