Programmers learn & share
0 votes

Problem :

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

by (6.9k points)   | 315 views

2 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:

by (36.1k points)  
edited by
0 votes


You'll find in the source tree esp826/modules. It would seem to be the low level driver for the flash memory - my guess is that the readblocks method is being called recursively perhaps owing to re-entrant code. When you change parameters is there a situation where a timer callback executes before another has finished accessing a file?

Accessing files in an interrupt handler is a bad idea because file access times are not deterministic. Further, code in interrupt handlers such as timer callbacks should be designed to execute fast.

I would probably avoid using a timer and just 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 increased the recursion limit by one and got the same exception. After increasing it to 5000 (1000 is the default) I no longer see it. Maybe I need to do some digging in my code since I am creating many temporary objects in each iteration of an event loop.

Found an infinite recursion issue in an overridden _getattr_ in my code and after fixing this I no longer see this exception. You may mark this bug as green or delete it.

ago by (5.5k points)  
2,187 questions
2,514 answers
241 users