Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Recursive call in Python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to achieve recursive calls in Python. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

When the function returns, get the return address from the top of the call stack, restore the scene, pop up the stack frame, and return by address

Recursive depth restriction in Python

The call stack also occupies memory, so it is impossible to set it too large, and there is a limit to the call stack size Python (the default is 1000 at the beginning)

This error often occurs when debugging recursive algorithms:

RecursionError: maximum recursion depth exceeded while calling a Python object

The reason for this error is that there are too many levels of recursion and the size of the system call stack is limited.

There are two reasons for this error:

Case 1: no basic end condition is set

Traceback (most recent call last): File "c:\ Users\ hy\ Desktop\ daily\ daily.py.py", line 15, in tell_story () File "c:\ Users\ hy\ Desktop\ daily\ daily.py.py", line 12, in tell_story tell_story () File "c:\ Users\ hy\ Desktop\ daily\ daily.py.py", line 12, in tell_story tell_story () File "c:\ Users\ hy\ Desktop\ daily\ daily.py.py", line 12 In tell_story tell_story () [Previous line repeated 993 more times] File "c:\ Users\ hy\ Desktop\ daily\ daily.py.py", line 11, in tell_story print ("there used to be a mountain There is a temple on the hill. There is an old monk in the temple. He said, ") RecursionError: maximum recursion depth exceeded while calling a Python object

06

Case 2: the program itself is right, but the evolution of the program to the basic end condition is too slow.

For example, the call stack depth is 1000, but now the sequence summation function calculates 2000 numbers, that's not good.

06

Adjust the call stack depth

The depth of the call stack can be adjusted by using the sys built-in module in python

Sys.getrecursionlimit () gets the call stack depth

Sys.setrecursionlimit (n) modify call stack depth

The above is how to achieve recursive calls in the Python shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report