Conditional Inheritance in Python
It happens most of the time that given a condition we need to decide whether a particular class should inherit a class or not, for example given a person, if he/she is eligible for an admission in a university only then they should be a student otherwise they should not be a student.
Let’s consider an example, where given a condition, we want a class (say C) to dynamically inherit from either class A or class B. We need to create two different classes C_A and C_B, which inherit from A and B respectively, However if the conditional inheritance is to either inherit or not based on a condition then we can use a different approach as discussed below
Example 1: Conditional Inheritance between 2 classes:
Create two classes C_A and C_B, and based on the condition return the respective class object.
True False False True
Example 2: For Either inheriting or not from A:
The approach is to use conditional statements while declaring the classes the given class C inherits. The below code executes and returns True
Example 3: The following code won’t run, as C does not inherit from A, thus has a default constructor that does not take any argument
TypeError Traceback (most recent call last)
<ipython-input-16-f0efc5a814d9> in <module>
18 # Object of C_A
—> 19 ca = C(1)
TypeError: object() takes no parameters