Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I inspect a Python's class hierarchy?

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?

like image 990
ElenaT Avatar asked Nov 20 '11 16:11

ElenaT


3 Answers

Hit f4 with class name highlighted to open hierarchy view.

like image 121
soulcheck Avatar answered Oct 14 '22 00:10

soulcheck


Try inspect.getclasstree().

like image 37
John Zwinck Avatar answered Oct 14 '22 00:10

John Zwinck


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'>)
like image 39
wkschwartz Avatar answered Oct 14 '22 00:10

wkschwartz