• Register
search
Log In
0 votes
347 views

Problem :

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

6.9k points

3 Answers

0 votes

Solution:

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:

sys.setrecursionlimit(limit):

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:

https://github.com/python/typing/issues/523

36.1k points
edited by
0 votes

Solution:

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:
		utime.sleep_ms(delta)

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.

12.8k points
edited by
1 vote

Solution:

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
sys.setrecursionlimit(10**6) 
def fact(n):
	if(n == 0):
		return 1
	return n * fact(n - 1)
if __name__ == '__main__':
	f = int(input('Enter the number: \n'))
	print(fact(f))

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.

ago 81k points

Related questions

0 votes
1 answer 36 views
36 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 24 views
24 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 5.7k points
0 votes
2 answers 720 views
720 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 452 views
452 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
1 vote
1 answer 35 views
35 views
Problem: I am trying to print some string in my python program but for some reason, it’s not working. The compiler says python syntaxerror: EOL while scanning string literal that is totally beyond my understandings. For obvious reason, I am avoiding my original code and ... test string" print(mystr) printStr() Here is the dummy code snippet. Please pull me up from the problem. Thanks to all.
asked Apr 7 Gavin 14.9k points