代码之家  ›  专栏  ›  技术社区  ›  user269044

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

  •  1
  • user269044  · 技术社区  · 16 年前

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

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

    6 回复  |  直到 16 年前
        1
  •  5
  •   Mike Graham    16 年前

    试着打电话 help 在你的课上。

        2
  •  4
  •   Nadia Alramli    16 年前

    请从命令行尝试以下操作:

    pydoc modulename
    
        3
  •  1
  •   mhawke    16 年前

    试试 help()

    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.
    
        4
  •  0
  •   John La Rooy    16 年前
        5
  •  0
  •   Gyuri    16 年前

    http://www.ibm.com/developerworks/library/l-pyint.html

    另一方面,为了进行调试,您可能只想将Eclipse与Python插件一起使用。

        6
  •  0
  •   Bill the Lizard    14 年前