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
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.
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.
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 .
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With