Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python class inheritance tree

Tags:

python

oop

Suppose I have this classes:

class a(object): 
    pass
class b(a):
    pass
class c(b): 
    pass
class d(c): 
    pass
class e(b):
    pass

I want a function that will do somthing like:

>>>get_ inheritance_tree(a)
>>>...b
>>>......c
>>>.........d
>>>......e
like image 703
Kipi Avatar asked Oct 26 '14 01:10

Kipi


People also ask

Is there class inheritance in Python?

Inheritance allows us to define a class that inherits all the methods and properties from another class. Parent class is the class being inherited from, also called base class. Child class is the class that inherits from another class, also called derived class.

How do you inherit a class from another class in Python?

To create a class that inherits from another class, after the class name you'll put parentheses and then list any classes that your class inherits from. In a function definition, parentheses after the function name represent arguments that the function accepts.

How do you check inheritance in Python?

Two built-in functions isinstance() and issubclass() are used to check inheritances. The function isinstance() returns True if the object is an instance of the class or other classes derived from it. Each and every class in Python inherits from the base class object .

What is __ bases __ in Python?

Python provides a __bases__ attribute on each class that can be used to obtain a list of classes the given class inherits. The __bases__ property of the class contains a list of all the base classes that the given class inherits.


1 Answers

Luckily for you, such visualization tools already exist.

One of them is epydoc. You can run a command like:

% epydoc -v --graph=classtree mymodule1.py [mymodule2.py ...]

Among the useful files it will generate, you'd find class-tree.html.

like image 79
shx2 Avatar answered Oct 08 '22 06:10

shx2