Conditional Inheritance in Python
Last Updated :
25 Aug, 2020
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.
Python3
class A(object):
def __init__(self, x):
self.x = x
def getX(self):
return self.X
class B(object):
def __init__(self, x, y):
self.x = x
self.y = y
def getSum(self):
return self.X + self.y
# inherits from A
class C_A(A):
def isA(self):
return True
def isB(self):
return False
# inherits from B
class C_B(B):
def isA(self):
return False
def isB(self):
return True
# return required Object of C based on cond
def getC(cond):
if cond:
return C_A(1)
else:
return C_B(1,2)
# Object of C_A
ca = getC(True)
print(ca.isA())
print(ca.isB())
# Object of C_B
cb = getC(False)
print(cb.isA())
print(cb.isB())
Output:
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
Python3
class A(object):
def __init__(self, x):
self.x = x
def getX(self):
return self.X
# Based on condition C inherits
# from A or it inherits from
# object i.e. does not inherit A
cond = True
# inherits from A or B
class C(A if cond else object):
def isA(self):
return True
# Object of C_A
ca = C(1)
print(ca.isA())
Output:
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
Python3
class A(object):
def __init__(self, x):
self.x = x
def getX(self):
return self.X
# Based on condition C inherits from
# A or it inherits from object i.e.
# does not inherit A
cond = False
## inherits from A or B
class C(A if cond else object):
def isA(self):
return True
# Object of C_A
ca = C(1)
print(ca.isA())
Output:
TypeError Traceback (most recent call last)
<ipython-input-16-f0efc5a814d9> in <module>
17
18 # Object of C_A
---> 19 ca = C(1)
20 print(ca.isA())
21
TypeError: object() takes no parameters
Similar Reads
Conditional Decorators in Python In Python, decorators are functions or classes that wrap around a function as a wrapper by taking a function as input and returning out a callable. They allow the creation of reusable building code blocks that can either change or extend behavior of other functions. Conditional Decorators Given a co
2 min read
Conditional Statements in Python Conditional statements in Python are used to execute certain blocks of code based on specific conditions. These statements help control the flow of a program, making it behave differently in different situations.If Conditional Statement in PythonIf statement is the simplest form of a conditional sta
6 min read
Code Golfing in Python Code Golf in Python refers to attempting to solve a problem using the least amount of characters possible. Like in Golf, the low score wins, the fewest amount of characters "wins". Python is a fantastic language for code golfing due to backward compatibility, quirks, it being a high-level language,
8 min read
Comparison Operators in Python Python operators can be used with various data types, including numbers, strings, boolean and more. In Python, comparison operators are used to compare the values of two operands (elements being compared). When comparing strings, the comparison is based on the alphabetical order of their characters
4 min read
Python | sympy.Interval().contains() method With the help of method, we can check whether a value is present in the interval or not by using sympy.Interval().contains() method. Syntax : sympy.Interval(x, y).contains(value) Return : Return the boolean value True or False. Example #1 : In this example we can see that by using sympy.Interval().c
1 min read