This is the code I am trying to execute, in Line 1
originally it was super().super().__init__()
which wasn't working (giving out error).
class A:
def __init__(self):
self.var1=5
class B(A):
def __init__(self):
self.var2=10
class C(B):
def __init__(self):
self.var3=20
super().__init__() ## Line 1
c=C()
print(c.var1)
EDIT - When I tried printing the variable of class A using object of class B it works fine
class A:
def __init__(self):
self.var1=5
class B(A):
def __init__(self):
self.var2=10
super().__init__()
b=B()
print(b.var1)
My question is why is c.var1
not giving out right output? (in both the cases, super().__init__()
and super().super().__init__()
and how can I access it?
Here is a little test for you that will help;
class A:
def __init__(self):
self.var1 = 5
print("In A")
class B(A):
def __init__(self):
self.var2 = 10
print("In B")
class C(B):
def __init__(self):
self.var3 = 20
print("In C")
super().__init__() ## Line 1
c = C()
will print;
In C
In B
but if you add a call in B to the constructor of A;
class A:
def __init__(self):
self.var1 = 5
print("In A")
class B(A):
def __init__(self):
self.var2 = 10
print("In B")
super().__init__()
class C(B):
def __init__(self):
self.var3 = 20
print("In C")
super().__init__() ## Line 1
c = C()
You get what you would expect;
In C
In B
In A
and this...
print(c.var1)
5