Recursion in Python? RuntimeError: maximum recursion depth exceeded when calling Python object

Possible duplicate:
Maximum recursion depth?

I have another problem with my code. I take my first program in Vpython, and I need to do a simulation of mixing two gases. At first I had a problem with the boundaries, but now that the balls (representing gas particles) remain within the boundaries, there are different errors. After a few seconds, I get an error message, which is shown below the source code of my function. The code:

def MovingTheBall(listOfBalls,position,numCell,flagOfExecution): flag = 0 if flagOfExecution==0: positionTmp = position else: positionTmp = (position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0) for i in range( 0, len(listOfBalls) ): if positionTmp==listOfBalls[i].pos: flag=1 if flag==1: return MovingTheBall(lista,(position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0),numCell,1) else: if positionTmp[0]==0 or positionTmp[0]>=numCell or positionTmp[0]<=-numCell or positionTmp[1]>=numCell or positionTmp[1]<=-numCell: return MovingTheBall(lista,(position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0),numCell,1) return positionTmp 

error:

  return MovingTheBall(listOfBalls,(position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0),numCell,1) File "gaz.txt", line 138, in MovingTheBall return MovingTheBall(listOfBalls,(position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0),numCell,1) File "gaz.txt", line 138, in MovingTheBall return MovingTheBall(listOfBalls,(position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0),numCell,1) File "gaz.txt", line 138, in MovingTheBall return MovingTheBall(listOfBalls,(position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0),numCell,1) File "gaz.txt", line 138, in MovingTheBall return MovingTheBall(listOfBalls,(position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0),numCell,1) File "gaz.txt", line 138, in MovingTheBall return MovingTheBall(listOfBalls,(position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0),numCell,1) File "gaz.txt", line 130, in MovingTheBall if positionTmp==listOfBalls[i].pos: RuntimeError: maximum recursion depth exceeded while calling a Python object 

Can anyone think of a way to simplify my function?

I run the it while loop function:

 while 1: rate(20) for i in range(0,len(self.listOfBalls)): self.listOfBalls[i].pos=poruszanie(self.listOfBalls,self.listOfBalls[i].pos,self.numCell,0) 
+8
source share
4 answers

Python does not have tail tail recursion optimization common in functional languages ​​like lisp. In Python, recursion is limited to 999 calls (see Sys.getrecursionlimit ).

If the depth of 999 is greater than you expect, check to see if the implementation has a condition that stops the recursion, or this test may not be correct in some cases.

I dare say that in Python, implementations of purely recursive algorithms are not correct / safe. The implementation of fib () limited to 999 is not entirely correct. You can always convert a recursive to iterative, and this is trivial.

This is not often achieved, because in many recursive algorithms, the depth tends to be logarithmic. If this is not the case with your algorithm, and you expect the recursion to be deeper than 999 calls, you have two options:

1) You can change the recursion limit with sys.setrecursionlimit(n) to the maximum allowable value for your platform:

sys.setrecursionlimit(limit) :

Set the maximum depth of the Python interpreter stack to limit. This limit prevents C stack overflows and Python crashes.

The maximum possible limit depends on the platform. The user may need to set a higher limit if he has a program that requires deep recursion and a platform that supports a higher limit. This should be done with caution, because a too high limit can lead to failure.

2) You can try to convert the algorithm from recursive to iterative. If the recursion depth is greater than your platform allows, this is the only way to solve the problem. There are step-by-step instructions on the Internet , and this should be a simple operation for a person with some background in CS. If you are having problems with this, post a new question so we can help.

+27
source

I changed recursion to iteration.

 def MovingTheBall(listOfBalls,position,numCell): while 1: stop=1 positionTmp = (position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0) for i in range(0,len(listOfBalls)): if positionTmp==listOfBalls[i].pos: stop=0 if stop==1: if (positionTmp[0]==0 or positionTmp[0]>=numCell or positionTmp[0]<=-numCell or positionTmp[1]>=numCell or positionTmp[1]<=-numCell): stop=0 else: return positionTmp 

Works well: D

+5
source

Error - stack overflow. That should call this site, right? This is because calling poruszanie leads to another call to poruszanie , increasing the recursion depth by 1. The second call leads to another call to the same function. This happens over and over again, each time increasing the depth of the recursion.

Now the used resources of the program are limited. Each function call takes up a certain amount of space on top of what is called a stack. If the maximum stack height is reached, you get an error.

+4
source

This error occurs when a function makes too many recursive calls for itself. This can be done because the base case is never executed (and therefore it gets stuck in an infinite loop) or just makes a lot of calls for itself. You can replace recursive calls with while loops.

0
source

All Articles