Q:

How to access cascaded class variable in Python

I have a program in which I need to access the variables and methods in the cascaded class. For example, how can I access the variable test and function a(), which is itself in classA, from classC.
class ClassA():
    def __init__(self, test):
        self.test = test
        print("Class A")

    def a(self):
        print("Method A")

class ClassB():

    def __init__(self):
        objecta = ClassA(test="Hello")
        print(objecta.test)
        print("Class A & B")

    def b(self):
        print("Method B")

class ClassC():
    def __init__(self):
        objectb = ClassB()
        objectb.a()
        print(objectb.test)
        print("Class A, B & C")

    def c(self):
        print("Method C")

c = ClassC()

I get a "AttributeError" while running this program.
Output:
Class A
Hello
Class A & B
Traceback (most recent call last):
  File "mytest/test_multi_class.py", line 31, in <module>
    c = ClassC()
  File "mytest/test_multi_class.py", line 24, in __init__
    objectb.a()
AttributeError: 'ClassB' object has no attribute 'a'

A:

You need to store the local variables objecta and objectb in the self reference if you want to use them after that instance is initialized.
class ClassA():
    def __init__(self, test):
        self.test = test
        print("Class A")

    def a(self):
        print("Method A")

class ClassB():

    def __init__(self):
        self.objecta = ClassA(test="Hello")
        print(self.objecta.test)
        print("Class A & B")

    def b(self):
        print("Method B")

class ClassC():
    def __init__(self):
        self.objectb = ClassB()
        self.objectb.objecta.a()
        print(self.objectb.objecta.test)
        print("Class A, B & C")

    def c(self):
        print("Method C")

c = ClassC()

alternatively, you can use object inheritance depending on whether it would make sense in your situation (I don't know from what you've posted so far.)
class AltA:
    def __init__(self, test):
        self.test = test
        self.a()
        print("Class A")

    def a(self):
        print("Method A")

class AltB(AltA):

    def __init__(self):
        super().__init__("Hello")
        print(self.test)
        self.a()
        self.b()
        print("Class A & B")

    def b(self):
        print("Method B")

class AltC(AltB):
    def __init__(self):
        super().__init__()
        self.a()
        self.b()
        self.c()
        print(self.test)
        print("Class A, B & C")

    def c(self):
        print("Method C")

c = AltC()

