用Python递归吗? RuntimeError:调用Python对象时超出了最大递归深度 [英] Recursion in Python? RuntimeError: maximum recursion depth exceeded while calling a Python object

查看:191
本文介绍了用Python递归吗? RuntimeError:调用Python对象时超出了最大递归深度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
最大递归深度?

Possible Duplicate:
Maximum recursion depth?

我的代码还有另一个问题. 我正在用Vpython编写我的第一个程序,我必须模拟两种气体的混合.首先,我对边界有问题,但是现在当球(代表气体粒子)停留在边界内时,有另一种错误.几秒钟后,我得到一个错误,该错误显示在我的函数的源代码下方. 代码:

I have another problem with my code. I'm wrtiting my first program in Vpython and I have to make a simulation of mixing two gases. First i had a problem with borders, but now when the balls(that represents the gas particles) stay within the borders there is sth different wrong. After a few seconds i get an error, which is shown below the source code of my function. 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

错误是:

    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 anybody think of a way to simplify my function?

我在while循环中运行该函数:

I run the function it while loop:

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)

推荐答案

Python缺少像lisp这样的功能语言中常见的尾部递归优化.在Python中,递归限制为999个调用(请参见 sys.getrecursionlimit ).

Python lacks the tail recursion optimizations common in functional languages like lisp. In Python, recursion is limited to 999 calls (see sys.getrecursionlimit).

如果999深度比您期望的要深,请检查实现是否缺少停止递归的条件,或者在某些情况下此测试可能是错误的.

If 999 depth is more than you are expecting, check if the implementation lacks a condition that stops recursion, or if this test may be wrong for some cases.

我敢说在Python中,纯递归算法实现不正确/不安全.限制为999的fib()实现不是真正正确的.总是有可能将递归转换为迭代,并且这样做是微不足道的.

I dare to say that in Python, pure recursive algorithm implementations are not correct/safe. A fib() implementation limited to 999 is not really correct. It is always possible to convert recursive into iterative, and doing so is trivial.

由于在许多递归算法中深度往往是对数的,因此通常无法达到.如果您的算法不是这种情况,并且您希望递归比999调用更深,那么您有两个选择:

It is not reached often because in many recursive algorithms the depth tend to be logarithmic. If it is not the case with your algorithm and you expect recursion deeper than 999 calls you have two options:

1)您可以使用sys.setrecursionlimit(n)更改递归限制,直到平台允许的最大值:

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

sys.setrecursionlimit(limit):

设置要限制的Python解释器堆栈的最大深度.此限制可防止无限递归导致C堆栈溢出和Python崩溃.

Set the maximum depth of the Python interpreter stack to limit. This limit prevents infinite recursion from causing an overflow of the C stack and crashing Python.

最大可能的限制取决于平台.当用户拥有需要深度递归的程序和支持更高限制的平台时,用户可能需要将限制设置为更高.这样做应该小心,因为上限太高会导致崩溃.

The highest possible limit is platform-dependent. A user may need to set the limit higher when she has a program that requires deep recursion and a platform that supports a higher limit. This should be done with care, because a too-high limit can lead to a crash.

2)您可以尝试将算法从递归转换为迭代.如果递归深度大于平台允许的深度,则这是解决问题的唯一方法.互联网上有分步说明对于受过CS教育的人来说,这应该是一个简单的操作.如果您对此有疑问,请发布一个新问题,以便我们提供帮助.

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. There are step by step instructions on the Internet and it should be a straightforward operation for someone with some CS education. If you are having trouble with that, post a new question so we can help.

这篇关于用Python递归吗? RuntimeError:调用Python对象时超出了最大递归深度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆