following is the two set of codes: one having constructor __init__(), and another having display() method.
code one:
class A(object):
def __init__(self):
self.a = "a"
print(self.a)
super().__init__()
class B(object):
def __init__(self):
self.b = "b"
print(self.b)
super().__init__()
class C(A,B):
def __init__(self):
self.c = "c"
print(self.c)
super().__init__()
o = C()
print(C.mro())
if we compile and run this above set of codes, output will be:
c
a
b
[<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>]
now, Second block of code is below,
class A(object):
def display(self):
self.a = "a"
print(self.a)
super().display()
class B(object):
def display(self):
self.b = "b"
print(self.b)
super().display()
class C(A,B):
def display(self):
self.c = "c"
print(self.c)
super().display()
o = C()
o.display()
print(C.mro())
if in above second block of code we replace __init__() with display() method then,
BECAUSE OF super().display() in class B, error will be shown
also if we remove super().display() in class B their will be no error
AttributeError: 'super' object has no attribute 'display'
which I understand, because OBJECT class does not have display() method in it.
but in the first block of code, the code runs without error.
Does that means OBJECT CLASS of python have __init__() in it?
if not, please explain me why their is no error in first block of code but their is error in second block of code?
object.__init__exists. It just doesn't do anything, butobjectis the "owner" of the method for purposes of allowing all classes to usesupercorrectly. (Conversely, if two different classes each introduce adisplaymethod, neither should usesuper, but anyone trying to inherit from both needs to designate one or the other as the "primary" owner ofdisplay, and adapt the other to usesuperappropriately.)