1

I am getting RecusionError in below program, I am not able to figure out why. Request help from experts.

calling method foo of Base class from Derived class results in RecusionError

BaseKill():
    def foo(self):
        print('what is wrong with jpython')
        return self.bar()
    def bar(self):
        print('just chill bar')
        return None

class Derived(BaseKill):
    def bar(self):
        print('I am bar')
        self.foo()
        self.hello()
    def hello(self):
        print('say Hi')


if __name__== '__main__':
    print('what')
    b=BaseKill()
    b.foo()
    c = Derived()
    c.bar()
3
  • 1
    BaseKill.foo() actually calls Derived.bar() when you call Derived.bar() in the first place, resulting in a infinit/recursion. Commented Apr 27, 2018 at 12:33
  • @EdwinvanMierlo thanks, but how I tell python to get bar from its own class. Commented Apr 27, 2018 at 12:40
  • answer updated with info on @staticmethod Commented Apr 27, 2018 at 13:12

2 Answers 2

1

When you do class Derived(BaseKill) the class Derived() now "inherrits" the methods from class BaseKill.

Including foo() and bar().

However you have bar() defined in class Derived() which takes precedent over the "inherrited bar()"

Basically this is similar as doing the following

class Derived():
    def foo(self):
        print('what is wrong with jpython')
        return self.bar()    
    def bar(self):
        print('I am bar')
        self.foo()
        self.hello()
    def hello(self):
        print('say Hi')


if __name__== '__main__':
    print('what')
    c = Derived()
    c.bar()

From this code you can clearly see that bar() is calling foo() which in turn is calling bar() again. This is an infinite/recursion.

The error you are seeing is correct.

In your original code you can include some print statements to get the name of the class represented by self, example print(self.__class__.__name__). This can help you further understand the class inheritance.

You might want to look at @staticmethod for BaseKill.bar(), however this will change the functionality, so ensure you test this thoroughly to ensure this is what you want:

class BaseKill():
    def foo(self):
        print('what is wrong with jpython')
        return BaseKill.bar()
    @staticmethod
    def bar():
        print('just chill bar')
        return None

class Derived(BaseKill):
    def bar(self):
        print('I am bar')
        self.foo()
        self.hello()
    def hello(self):
        print('say Hi')


if __name__== '__main__':
    print('what')
    b=BaseKill()
    b.foo()
    c = Derived()
    c.bar()

More information about @staticmethod here

Alternatively you can do the following for foo() in BaseKill:

class BaseKill():
    def foo(self):
        print('what is wrong with jpython')
        return BaseKill.bar(self)
Sign up to request clarification or add additional context in comments.

Comments

0

Leason learned: while calling method of same class, use its name in-front of them, like BaseKill.bar(self) to prevent error due to overriding of methods

BaseKill():
        def foo(self):
            print('what is wrong with jpython')
            #return self.bar() ----Error 'self' referred to Calling class
            return BaseKill.bar(self) #referred to same class method

        def bar(self):
            print('just chill bar')
            return None

    class Derived(BaseKill):
        def bar(self):
            print('I am bar')
            self.foo()
            self.hello()
        def hello(self):
            print('say Hi')


    if __name__== '__main__':
        print('what')
        b=BaseKill()
        b.foo()
        c = Derived()
        c.bar()

Comments

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.