


1. Initial recursion
Recursive function: Call the function itself within a function.
Maximum depth of recursion: 998
As you just saw, the recursive function will continue to execute if it is not blocked by external forces. But we have already talked about the problem of function calls before. Each function call will generate a name space of its own. If it is called continuously, it will cause the name space to occupy too much memory, so Python is trying to prevent this kind of phenomenon. , forcibly controlling the number of recursion levels to 997 (as long as 997! You can’t suffer losses or be fooled...).
What can be used to prove this "998 theory"? Here we can do an experiment:
def foo(n): print(n) n += 1 foo(n) foo(1)
From this we can see that the maximum number that can be seen before an error is reported is 998. Of course, 997 is a number set by python for the memory optimization of our program. Default value, of course we can also modify it through some means:
import sys print(sys.setrecursionlimit(100000))
We can modify the maximum depth of recursion in this way. We just set the recursion depth allowed by python to 10w. As for the actual recursion depth, it can be reached The depth depends on the performance of the computer. However, we still do not recommend changing this default recursion depth, because if the problem cannot be solved with 997 levels of recursion, it is either not suitable to use recursion to solve it, or your code is too poorly written~~~
Look At this point, you may feel that recursion is not such a good thing, and it is not as useful as while True! However, there is a saying circulating in the world: humans understand cycles, gods understand recursion. So don’t underestimate recursive functions. Many people have been blocked from the threshold of great masters for so many years because they failed to understand the true meaning of recursion. And many of the algorithms we learn in the future will be related to recursion. Come on, only if you learn it will you have the capital to dislike it!
2. Recursive example explanation
Here we will give another example to illustrate what recursion can do.
Example 1:
Now you ask me, how old is Mr. Alex? I said I won't tell you, but Alex is two years older than egon.
If you want to know how old Alex is, do you still have to ask Egon? egon said, I won’t tell you either, but I am two years older than Sir Wu.
You asked Sir Wu again, but Sir Wu didn’t tell you either. He said he was two years older than Taibai.
Then you ask Taibai, Taibai will tell you that he is 18.
Did you know it at this time? How old is alex?
1 | jinxin | 18 |
---|---|---|
武 sir | 20 | |
egon | 22 | |
alex | 24 |
The above is the detailed content of Python recursive function, introduction to binary search algorithm. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
