1

I have the following code , where I want to achieve the following: Please Understand I am a newbie in OOP 1) Initialize "class B" from main function

2) Inherit "class A" from "class B" and depending upon some conditions of variables of "class A" call "class D" from within "class B"

3) After call of "class D" use variables from "class A" to modify them and print it out.

4) After that go back to "class B" to run code for more conditions.

```

    class A(object):
        def __init__(self):
            super(A,self).__init__()
            self.A_var = 0
            print('Running A.__init__')
    class B(A):
        def __init__(self):
            super(B,self).__init__()
            #B.__init__(self)

        def fun2():
            self.A_var += 5
            #foo = D()
            self.fun1
            print('Running B.__init__')
            print self.A_var        

            #A.__init__(self) 


    class D(B,A):
        def __init__(self):
            #super(A,grandfather).__init__()
            #A.__init__(grandfather)
            super(D,self).__init__()
            #D.__init__(self)
        def fun1():
            print('Running D.__init__')
            self.A_var += 400
            print self.A_var


    if __name__ == '__main__':
        b = B()
        b.fun2

```

I want to print out the value of "self.A_var" from inside "class D"

2
  • What is your question? Commented May 26, 2016 at 7:55
  • @barny I want it to print out "self.A_var" , but it does not give me the required output , instead it gives "Running A.__init__" as output. Commented May 26, 2016 at 8:04

2 Answers 2

1

if you want to print out the value of "self.A_var" from inside "class D" than execute : d.fun1() checkout this modified code:

class A(object):
    def __init__(self):
        super(A, self).__init__()
        self.A_var = 0
        print('Running A.__init__')


class B(A):
    def __init__(self):
        super(B, self).__init__()

    def fun2(self):
        self.A_var += 5
        print ('Running B.__init__'), self.A_var


class D(B):
    def __init__(self):
        super(D, self).__init__()

    def fun1(self):
        self.A_var += 400
        print ('Running D.__init__'), self.A_var


if __name__ == '__main__':
    d = D()
    d.fun2()
    d.fun1()

with this code output is

Running A.__init__
Running B.__init__ 5
Running D.__init__ 405 
Sign up to request clarification or add additional context in comments.

3 Comments

Can you also tell how can I call "class D" from within "class B" i.e from within "fun2" depending upon a condition. Thanks for the answer too.
@RishabhGupta please let me clear your condition what exactly you want to do after calling class D in class B fun2 function. and if you call Class D in Class B than you will get - RuntimeError: maximum recursion depth exceeded while calling a Python object
According to me this should be the flow of the program : 1)The main program should call "class B" which must inherit "class A" and all its attributes/variables. 2) Depending upon the state of a variable of "class A" , "class B" should call "class D" which must have all the variables of "class A" and "class B" 3) After that the code must return to "class B" so that I can check additional conditions to call similar classes to "class D" from inside "class B" Thanks for helping me out.
0

You haven't understood what inheritance is. An instance of D is an instance of A and B, because it inherits from them. The only thing you need to do is make sure you call super() in each __init__ method; for some reason you've replaced the one in D with the totally wrong D.__init__, which will endlessly recurse. Put back super(D,self).__init__().

The only other change is to remove the pointless instantiation of D within B.fun2; just call self.fun1() there.

1 Comment

I still don't get the required output of outputting the "self.A_var" , how do I go about that , instead I just get Running A.__init__ as outpur , please reply. thanks for understanding , I am a newbie in OOP in python.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.