• Register
0 votes

Problem :

I am getting below error
maximum recursion depth exceeded while calling a python object

6 5 3
6,930 points

3 Answers

0 votes


Python lacks the tail recursion optimizations which is common in functional languages like lisp. In Python, recursion is limited to 999 calls 

1) You can change the recursion limit with sys.setrecursionlimit(n) until the maximum allowed for your platform:


2) You can try to convert the algorithm from recursive to iterative. If recursion depth is bigger than allowed by your platform, it is the only way to fix the problem. 

Further Readings:


9 7 4
38,600 points
0 votes


You'll trace flashbdev.py in the source tree esp826/modules. It would appear to be the low level driver for the flash memory - my guess is that the readblocks system is being named recursively probably owing to re-entrant code. At the time you change parameters is there a situation where a timer callback implements prior another has finished accessing a file?

Accessing files in an interrupt handler is a bad idea since file access times are not deterministic. More, code in interrupt handlers such as timer callbacks must be designed to implement fast.

I would possibly avoid employing a timer and only run a loop which achieves its timing like this:

import utime
while True:
	tstart = utime.ticks_ms()
	# do all your stuff
	delta = 150 - utime.ticks_diff(utime.ticks_ms(), tstart)
	if delta > 0:

I raised the recursion limit by one and got the similar exception. After rising it to 5000 (1000 is the default) I no longer view it. Maybe I require to do few digging in my code because I am making many temporary objects in each iteration of an event loop.

Figurred out an infinite recursion problem in an overridden _getattr_ in my code and after solving this I no longer view this exception. You may mark this bug as green or delete it.

10 6 4
31,120 points
1 vote


When you run a recursive function in Python on a large input ( > 10^4), you might face a “maximum recursion depth exceeded error”. This is a common scenario for a program when someone tries to run a program with a more extensive input range. To avoid the error, you can call the setrecursionlimit() function from the sys module in Python. This particular function modifies the recursion limit in Python. Using this function, we can set your recursion limit 10**6 to avoid the error. Let’s see how it works:

import sys
def fact(n):
	if(n == 0):
		return 1
	return n * fact(n - 1)
if __name__ == '__main__':
	f = int(input('Enter the number: \n'))

Boom! Now you’ve increased the limit, and the program can calculate the factorial up to 10**6 range number. I hope this example will help you to understand.

13 9 6
94,240 points

Related questions

0 votes
1 answer 44 views
Problem : I am trying to write my first program in Vpython and I have to make the simulation of mixing two gases. First I had the problem with borders, but now when my balls(that represents a gas particles) stay within a border there is the sth different wrong. ... ) for j in range(0,len(self.listOfBalls)): self.listOfBalls[j].pos=poruszanie(self.listOfBalls,self.listOfBalls[j].pos,self.numCell,0)
asked Jan 23 jwilliam 3.9k points
0 votes
1 answer 37 views
Problem: I am new to python programming and I am little bit bit confused about some things like I wrote a code: def random(): a = random.randint (0, x+1) b = random.randint (0, y+1) c = random.randint (0, z+1) outer_function(a,b,c ... recursion depth exceeded while calling a Python object IndentationError: expected an indented block I am confused about those things, why is this error shown to me ?
asked Mar 22 LizzyM 6.1k points
0 votes
1 answer 1.2K views
Problem : I had to remove a +CompatEnvVars from following : SSLOptions +FakeBasicAuth +ExportCertData +CompatEnvVars +StrictRequire As it said it was an invalid command or something. So having removed that and following a instructions to a nail it get below error: ... maximum permissible length. (Error code: ssl_error_rx_record_too_long) I am very new to SSL, any advice on what is going wrong?
asked Jan 17 jwilliam 3.9k points
0 votes
2 answers 932 views
Problem : I am new to Py 3.5. This code was working properly in Python 2.7: with open(fname, 'rb') as f: lines = [x.strip() for x in f.readlines()] for line in lines: tmp = line.strip().lower() if 'some-pattern' in tmp: continue # ... code After upgrading to 3.5, I'm getting the: TypeError: a bytes-like object is required, not 'str'
asked Oct 22, 2019 peterlaw 6.9k points
0 votes
1 answer 5 views
Problem: hello Everyone, We are working on the firebase based real-time messaging application and we are encountering this error whenever we try to run our application. The compiler we are using is Xode11 and the language version is swift 5. Your valuable response is welcomed. Thanks
asked Oct 12 SapperIt 1.2k points