Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum recursion depth error in Python when calling super's init. [duplicate]

I have a class hierarchy A <- B <- C, in B, I need some processing in the constructor, so I came up with this code from this post: Understanding Python super() with __init__() methods

#!/usr/bin/python

class A(object):
    def __init__(self, v, v2):
        self.v = v
        self.v2 = v2

class B(A):
    def __init__(self, v, v2):
        # Do some processing
        super(self.__class__, self).__init__(v, v2)

class C(B):
    def hello():
        print v, v2


b = B(3, 5)
print b.v
print b.v2

c = C(1,2)
print c

However, I have an runtime error from maximum recursion exceeded

  File "evenmore.py", line 12, in __init__
    super(self.__class__, self).__init__(v, v2)
RuntimeError: maximum recursion depth exceeded while calling a Python object

What might be wrong?

like image 584
prosseek Avatar asked Sep 28 '15 20:09

prosseek


People also ask

How do I fix maximum recursion depth exceeded in Python?

The maximum recursion depth in Python is 1000. You can change the limit by calling sys. setrecursionlimit() method.

How do you fix maximum recursion depth exceeded in comparison?

The “maximum recursion depth exceeded in comparison” error is raised when you try to execute a function that exceeds Python's built in recursion limit. You can fix this error by rewriting your program to use an iterative approach or by increasing the recursion limit in Python.

What is the maximum depth of recursive calls a function?

The maximal number of nested calls (including the first one) is called recursion depth. In our case, it will be exactly n . The maximal recursion depth is limited by JavaScript engine. We can rely on it being 10000, some engines allow more, but 100000 is probably out of limit for the majority of them.

What is a recursion limit?

$RecursionLimit gives the maximum length of the stack returned by Stack[]. Each time the evaluation of a function requires the nested evaluation of the same or another function, one recursion level is used up. On most computers, each level of recursion uses a certain amount of stack space.


2 Answers

First thing to consider: C inherits constructor from B (because it's not defined in C).

Second thing to consider: self.__class__ in __init__ invocation in C class is C, not B.

Let's analyze:

  • C().__init__ calls super(self.__class__, self).__init__(v, v2) which is resolved to super(C, self).__init__(v, v2) which means B.__init__(self, v, v2).
  • First argument passed to B.__init__ has a type C. super(self.__class__, self).__init__(v, v2) is again resolved to B.__init__(self, v, v2).
  • And again, and again, and again. And there is your infinite recursion.
like image 164
Łukasz Rogalski Avatar answered Oct 12 '22 02:10

Łukasz Rogalski


Giving the first parameter of super as the class name solves this issue.

class B(A):
    def __init__(self, v, v2):
        # Do some processing
        super(B, self).__init__(v, v2)
like image 22
prosseek Avatar answered Oct 12 '22 00:10

prosseek