0

I have created 3 classes which are as follows:

  1. A having a1,a2 as class vairiables
  2. B(A) A is the parent for B, having b1,b2 as class Variables
  3. C(B) B is the parent for C, having c1, c2 as the class Variables

I am trying to create an Object for the C(B) class by typing:- obj = C(1,2)

But I am not able to access the class A, B variables (a1,a2,b1,b2) using the object created from the C class.

class A:
    def __init__(self,a1,a2):
        self.a1 = a1
        self.a2 = a2
    def testa():
        print('inside A')

class B(A):
    def __init__(self,b1,b2):
        self.b1 = b1
        self.b2 = b2
    def testb():
        print('inside B')

class C(B):
    def __init__(self,c1,c2):
        self.c1 = c1
        self.c2 = c2
    def testc():
        print('inside C')


obj = C(1,2)
Michael Ruth
  • 2,938
  • 1
  • 20
  • 27
  • 2
    Did you know that the parent `__init__` is not automatically called if you override it in the subclass? – mkrieger1 Dec 27 '22 at 20:16
  • 1
    You would have the same problem with a single level of inheritance, it has nothing to do with multiple inheritance levels. – mkrieger1 Dec 27 '22 at 20:19
  • Does this answer your question? [How to invoke the super constructor in Python?](https://stackoverflow.com/questions/2399307/how-to-invoke-the-super-constructor-in-python) – mkrieger1 Dec 27 '22 at 20:23
  • 3
    I'd strongly suggest to read about inheritance and class/instance variables – Gameplay Dec 27 '22 at 20:53
  • 1
    What's your question exactly? What output were you expecting when you tried to access `obj.a1`, etc? Please make a [mre]. It seems like you thought the parent `__init__()` would be called automatically, but it's not clear what values you would think it would use. On the other hand, those are actually **instance variables**, not class variables like you're saying, so if they're actually supposed to be class variables, that's a very different question. BTW, check out [ask] if you want general tips. – wjandrea Dec 27 '22 at 21:01
  • The earlier comments have alluded to it, but let me state more clearly: None of the variables you've defined in any of your classes are class variables. That term in Python is used to describe what are called static variables in C++ and Java, variables who's values are shared by all instances of a class. That distinction doesn't really matter for your question (which is about failing to set some of the instance variables), but might help you search and communicate about the issue you're actually having. – Blckknght Dec 27 '22 at 21:15

1 Answers1

0

You have to make the classes communicate. Here are two alternatives.

Alternative 1

class A:
    def __init__(self, a1, a2): 
        self.a1 = a1
        self.a2 = a2
        print('inside A')

class B(A):
    def __init__(self, b1, b2):
        super().__init__(b1, b2)
        self.b1 = b1
        self.b2 = b2
        print('inside B')

class C(B):
    def __init__(self, c1, c2):
        super().__init__(c1, c2)
        self.c1 = c1
        self.c2 = c2
        print('inside C')


obj = C(1, 2)
print(obj.a2, obj.a1)  # -> 2 1
print(obj.b2, obj.b1)  # -> 2 1
print(obj.c2, obj.c1)  # -> 2 1

Alternative 2

class A:
    def __init__(self, a1, a2):
        self.a1 = a1
        self.a2 = a2
        print('inside A')

class B(A):
    def __init__(self, b1, b2, a1, a2):
        A.__init__(self, a1, a2)
        self.b1 = b1
        self.b2 = b2
        print('inside B')

class C(B):
    def __init__(self, c1, c2, b1, b2, a1, a2):
        B.__init__(self, b1, b2, a1, a2)
        self.c1 = c1
        self.c2 = c2
        print('inside C')


obj = C(6, 5, 4, 3, 2, 1)
print(obj.a2, obj.a1)  # -> 1 2
print(obj.b2, obj.b1)  # -> 3 4
print(obj.c2, obj.c1)  # -> 5 6
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Igor Micev
  • 1,514
  • 1
  • 17
  • 23