I’ve taken over some object-oriented Python code or questionable origin that I didn’t write and don’t fully understand. I have a question on object oriented inheritance behavior in Python that I’m seeing in this unfamiliar code. Here’s a minimal example of the issue that I’m struggling to understand:
class A:
def __init__(self):
self.a = "a"
print("A.__init__()")
class B:
def __init__(self):
self.b = "b"
print("B.__init__()")
class C:
def __init__(self):
self.c = "c"
print("C.__init__()")
class X(A):
def __init__(self):
A.__init__(self)
B.__init__(self) # Note: B is not in X's inheritance list.
C.__init__(self) # Note: C is not in X's inheritance list.
print("X.__init__()")
x = X()
If I run the code, it seems to run fine, and produces the expected output, in the expected order.
Question: What was the author’s intent when calling external initializers that are seemingly unrelated to the direct inheritance? Is this a reasonable thing to do in Python, and is there a common use case that involves this type of initialization pattern?
I don’t know why you would do this in Python.
nehfan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3