我在 VS 代碼上做一個 pygame 專案,不小心寫了以下代碼:
def appear():
end()
def end():
appear()
我的pylance沒有顯示錯誤。我想知道為什么在第 2 行它沒有顯示“未定義結束”。
當我在一個單獨的 python 檔案中運行這段代碼時:
def appear():
end()
def end():
appear()
appear()
解釋器也沒有顯示 NameError,而是在四到五秒后顯示 RecursionError,如下所示:
...
File "c:/Users/.../test.py", line 5, in end
appear()
File "c:/Users/.../test.py", line 2, in appear
end()
File "c:/Users/.../test.py", line 5, in end
appear()
File "c:/Users/.../test.py", line 2, in appear
end()
File "c:/Users/.../test.py", line 5, in end
appear()
File "c:/Users/.../test.py", line 2, in appear
end()
File "c:/Users/.../test.py", line 5, in end
appear()
File "c:/Users/.../test.py", line 2, in appear
end()
RecursionError: maximum recursion depth exceeded
這是什么意思??
uj5u.com熱心網友回復:
在 Python 中,函式必須在呼叫之前定義(執行,而不僅僅是在另一個定義中使用)。因此,您的函式定義沒有問題。好吧,除了創建一個無限遞回回圈......當你執行它時,Python解釋器提供的錯誤(“RecursionError:超出最大遞回深度”)發生是因為Python只允許一定級別的遞回,我猜是如果我沒記錯的話,默認為 1000(可以更改)。由于您的函式無限期地相互呼叫,所有可能的遞回都在這 4 秒內完成。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/482874.html
下一篇:Python例外中的奇怪布林值
