在Python中对无限循环使用无限递归是一种罪过吗?

时间:2022-12-15 05:33:33

This question is more about curiosity than utility. If I'm writing a function that's supposed to run for ever, for instance a daemon, how would Python handle it if I called the function again from the end of the function?

这个问题更多的是关于好奇心而不是效用。如果我正在编写一个应该永远运行的函数,例如一个守护进程,如果我在函数末尾再次调用这个函数,Python将如何处理它?

def daemonLoop():

    # Declare locals

    # Do stuff

    daemonLoop()

I'm fairly sure that doing this in C would result in a stack overflow, but given the level of abstraction from C to Python I'm guessing stuff is handled differently.

我相当肯定在C中这样做会导致堆栈溢出,但是考虑到从C到Python的抽象级别,我猜事情的处理方式是不同的。

Would I go to hell for this?

我会因此而下地狱吗?

6 个解决方案

#1


17  

In almost all Python interpreters that will cause a stack overflow, as it would in C. The higher-level feature that would permit this is called Tail Call Optimization or Tail Recursion Elimination, and the benevolent dictator of Python opposes adding this to the language.

在几乎所有会导致堆栈溢出的Python解释器中(就像在c中那样),允许这种情况发生的高级特性称为尾部调用优化或尾部递归消除,而Python的仁慈*者则反对将其添加到语言中。

This style is considered to be non-idiomatic for Python, and a simple while True: loop is preferred.

这种风格被认为是Python的非惯用风格,最好是简单而真实的:loop。

#2


5  

The maximum recursion depth can be retrieved with sys.getrecursionlimit() and set with sys.setrecursionlimit().

可以使用sys.getrecursionlimit()检索最大递归深度,并使用sys.setrecursionlimit()设置最大递归深度。

Would I go to hell for this?

我会因此而下地狱吗?

Yes. CPython has no Tail Recursion Elimination / Last Call Optimization.

是的。CPython没有尾递归消除/最后调用优化。

def recurse():
    recurse()

recurse()

Error:

错误:

  # 1000 or so lines of this:
  File "", line 2, in recurse
RuntimeError: maximum recursion depth exceeded

#3


1  

The C version of the Python interpreter (which is probably what you're using) will raise an error eventually if you never return from daemonLoop. I'm not sure about other versions.

如果您从未从daemonLoop返回,那么Python解释器的C版本(可能就是您正在使用的)将最终导致错误。我不确定其他版本。

#4


1  

I don't know why you'd think about doing something like that when you could simply have an infinite while loop. Anyway for your question about whether it will work:

我不知道为什么你会想做这样的事情当你可以有一个无限的while循环。不管怎样,你的问题是它是否有效:

...
  File "test.py", line 7, in daemonLoop
    daemonLoop()
  File "test.py", line 7, in daemonLoop
    daemonLoop()
RuntimeError: maximum recursion depth exceeded

So yeah, hell it is.

是的,该死的。

#5


0  

Probably not a good idea....

可能不是一个好主意....

def forever(): forever()

forever()

RuntimeError: maximum recursion depth exceeded

运行时错误:超过最大递归深度

http://paulbarry.com/articles/2009/09/02/infinite-recursion

http://paulbarry.com/articles/2009/09/02/infinite-recursion

#6


0  

(define forever (lambda () (forever)))

This kind of recursion is what Lisp dialects like Scheme are made for!

这种递归是Lisp方言如Scheme的用途!

#1


17  

In almost all Python interpreters that will cause a stack overflow, as it would in C. The higher-level feature that would permit this is called Tail Call Optimization or Tail Recursion Elimination, and the benevolent dictator of Python opposes adding this to the language.

在几乎所有会导致堆栈溢出的Python解释器中(就像在c中那样),允许这种情况发生的高级特性称为尾部调用优化或尾部递归消除,而Python的仁慈*者则反对将其添加到语言中。

This style is considered to be non-idiomatic for Python, and a simple while True: loop is preferred.

这种风格被认为是Python的非惯用风格,最好是简单而真实的:loop。

#2


5  

The maximum recursion depth can be retrieved with sys.getrecursionlimit() and set with sys.setrecursionlimit().

可以使用sys.getrecursionlimit()检索最大递归深度,并使用sys.setrecursionlimit()设置最大递归深度。

Would I go to hell for this?

我会因此而下地狱吗?

Yes. CPython has no Tail Recursion Elimination / Last Call Optimization.

是的。CPython没有尾递归消除/最后调用优化。

def recurse():
    recurse()

recurse()

Error:

错误:

  # 1000 or so lines of this:
  File "", line 2, in recurse
RuntimeError: maximum recursion depth exceeded

#3


1  

The C version of the Python interpreter (which is probably what you're using) will raise an error eventually if you never return from daemonLoop. I'm not sure about other versions.

如果您从未从daemonLoop返回,那么Python解释器的C版本(可能就是您正在使用的)将最终导致错误。我不确定其他版本。

#4


1  

I don't know why you'd think about doing something like that when you could simply have an infinite while loop. Anyway for your question about whether it will work:

我不知道为什么你会想做这样的事情当你可以有一个无限的while循环。不管怎样,你的问题是它是否有效:

...
  File "test.py", line 7, in daemonLoop
    daemonLoop()
  File "test.py", line 7, in daemonLoop
    daemonLoop()
RuntimeError: maximum recursion depth exceeded

So yeah, hell it is.

是的,该死的。

#5


0  

Probably not a good idea....

可能不是一个好主意....

def forever(): forever()

forever()

RuntimeError: maximum recursion depth exceeded

运行时错误:超过最大递归深度

http://paulbarry.com/articles/2009/09/02/infinite-recursion

http://paulbarry.com/articles/2009/09/02/infinite-recursion

#6


0  

(define forever (lambda () (forever)))

This kind of recursion is what Lisp dialects like Scheme are made for!

这种递归是Lisp方言如Scheme的用途!