So, I know I can use dir() to get information about class members etc. What I'm looking for is a way to get a nice开发者_如何学编程ly formatted report on everything related to a class (the members, docstrings, inheritance hierarchy, etc.).
I want to be able to run this on the command-line so I can explore code and debug better.
Try calling help
on your class.
Try this from the command line:
pydoc modulename
Try the help()
facility that is built into the interpreter. 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)
This works for just about anything, e.g.
>>> 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.
try ipython
You want introspection. This is a good article about all that: http://www.ibm.com/developerworks/library/l-pyint.html
On the other hand, for debugging, you might just want to use Eclipse with the Python plugin.
Not sure exactly what you need, but this chapter from Mark Pilgrim's "Dive into Python" might work.
精彩评论