2010-02-08 3 views
1

Donc, je sais que je peux utiliser dir() pour obtenir des informations sur les membres de classe, etc. Ce que je cherche est un moyen d'obtenir un rapport bien formaté sur tout ce qui concerne une classe (membres, docstrings, hiérarchie d'héritage, etc.). Je veux être en mesure d'exécuter ceci sur la ligne de commande afin que je puisse explorer le code et mieux déboguer.Y a-t-il un moyen pour moi d'obtenir des informations formatées détaillées sur une classe Python?

Répondre

5

Essayez d'appeler help sur votre cours.

+4

Également, écrivez des docstrings pour votre classe, ce qui rend l'aide encore plus belle. –

+0

Exactement ce que je voulais. Merci! – user269044

4

Essayez ceci de la ligne de commande:

pydoc modulename 
0

Je ne sais pas exactement ce dont vous avez besoin, mais this chapter de "Dive en Python" de Mark Pilgrim pourrait fonctionner.

1

Essayez l'installation help() qui est intégré dans l'interpréteur. Par exemple.

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) 

Cela fonctionne pour à peu près n'importe quoi, par ex.

>>> 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. 
Questions connexes