10

Assuming I have a class X, how do I check which is the base class/classes, and their base class/classes etc?

I'm using Eclipse with PyDev, and for Java for example you could type CTRL + T on a class' name and see the hierarchy, like:

java.lang.Object
   java.lang.Number
       java.lang.Integer

Is it possible for Python?

If not possible in Eclipse PyDev, where can I find this information?

soulcheck
  • 36,297
  • 6
  • 91
  • 90
ElenaT
  • 2,600
  • 4
  • 24
  • 41
  • 2
    This is answered in [Python: List all base classes in a hierarchy](http://stackoverflow.com/questions/1401661/python-list-all-base-classes-in-a-hierarchy) – kojiro Nov 20 '11 at 16:42

5 Answers5

8

Hit f4 with class name highlighted to open hierarchy view.

soulcheck
  • 36,297
  • 6
  • 91
  • 90
7

Also, every class carries around with it an attribute called __mro__ which gives all the parent classes from which a given class could inherit methods or attributes. Read them from left to right. For example:

assert bool.__mro__ == (<class 'bool'>, <class 'int'>, <class 'object'>)
assert True.__class__.__mro__ == (<class 'bool'>, <class 'int'>, <class 'object'>)
wkschwartz
  • 3,817
  • 2
  • 29
  • 33
  • 2
    Also every (new style) class has a `mro` method, so you can call `bool.mro()` and `True.__class__.mro()` respectively (it returns list instead of tuple). – derenio Oct 01 '15 at 11:02
7

Try inspect.getclasstree().

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1

You can also visualize class hierarchy as a picture.
I use Graphviz DOT and pyreverse to visualize and explore a large mess of classes (an example).

Assuming you want to draw all classes from tree.py in parso library:

# Generate classes_tree_full.png with full UML-style class boxes
$ pyreverse -mn    -o png -p tree_full  Lib\site-packages\parso\python\tree.py

# Generate classes_tree_short.png with only class names in boxes
$ pyreverse -mn -k -o png -p tree_short Lib\site-packages\parso\python\tree.py

Change -mn to -my if you want to see qualified class names, ex. parso.python.tree.Literal instead of just Literal.
Note: pyreverse also supports multiple files via globpatterns.

Samantra
  • 65
  • 1
  • 8
0

Hit command+o, then Press Ctrl+O to show parent hierarchy

checkout this blog http://pydev.blogspot.jp/2015/03/navigating-through-your-code-when-in.html

wang binghuan
  • 63
  • 1
  • 4