递归思考 Python 2 练习 5.5 [英] recursion Think Python 2 exercise 5.5

查看:40
本文介绍了递归思考 Python 2 练习 5.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于 Think Python 2 练习 5.5 的这个问题已经已经询问并回答了,但我仍然不明白这个功能是如何工作的.这是有问题的函数:

This question regarding exercise 5.5 from Think Python 2 has been asked and answered already, but I still don't understand how this function works. Here's the function in question:

def draw(t, length, n):
    if n == 0:
        return
    angle = 50
    t.fd(length*n)
    t.lt(angle)
    draw(t, length, n-1)
    t.rt(2*angle)
    draw(t, length, n-1)
    t.lt(angle)
    t.bk(length*n)

我在这里看到一个递归循环:

I see a recursive loop here:

def draw(t, length, n):
    if n == 0:
        return
    angle = 50
    t.fd(length*n)
    t.lt(angle)
    draw(t, length, n-1)

在函数调用自身足够次数导致 n == 0 之后,它如何进入下一步,即 t.rt(2*angle)?一旦 n == 0,函数不应该返回 None 并简单地结束吗?如果没有,返回到哪里?下一次调用自身时,同样的事情再次发生,然后函数在满足状态 n == 0 很久之后继续执行工作(两次!).我显然在这里遗漏了一个关键概念,并且希望任何人都可以就这个主题提供任何线索.先谢谢了~

After the function calls itself a sufficient number of times to cause n == 0, how does it move on to the next step, namely t.rt(2*angle)? Once n == 0, shouldn't the function return None and simply end? If not, where is returning to? The same thing happens again the next time it calls itself, and then the function continues to perform work long after the state n == 0 has been satisfied (twice!). I'm obviously missing a key concept here and would appreciate any light anyone can shed on this topic. Thanks in advance~

推荐答案

return 语句将程序的执行返回到调用返回函数的行.

The return statement returns execution of the program back to the line where the function that returned was called.

如果您不熟悉调用堆栈是什么,可以把它想象成一个函数堆栈——每当有东西被调用时,它就会被放在堆栈的顶部,这就是正在运行的东西.当它返回时,它被从堆栈中移除,因此调用它的函数(堆栈中的下面一个)现在是顶部,所以这就是执行再次开始的地方.

If you're not familiar with what a call stack is, think of it as a stack of functions - whenever something gets called, it's placed on top of the stack and that's what's running. When that returns, it's removed from the stack and so the function that called it (one below in the stack) is now the top, so that's where execution picks up again.

该特定函数将在 n == 0 时返回(所以这是您的基本情况),因此一旦它的递归调用 draw(t, length, n-1) - 因此,它的随后的递归调用,因为直到它们完成它才会完成执行 - 到达那个点,它会移到下一行.

That particular function will return when n == 0 (so that's your base case), so once its recursive call draw(t, length, n-1) - and, by consequence, its subsequent recursive calls, since it won't finish execution until they do - reach that point, it'll move over to the next line.

采用更简单的函数 foo(sum, n) 代替:

Take a simpler function foo(sum, n) instead:

def foo(sum, n):
    if (n == 0):
        return sum
    sum += foo(sum, n-1)
    sum /= 2
    return foo(sum, n-1)

无论何时调用它,它都不会从 sum += foo(sum, n-1) 移动,直到该调用和所有递归调用返回为止.此功能在结构上与您展示的功能相当,它可能有助于您更好地想象正在发生的事情.

Whenever you call that, it won't move from sum += foo(sum, n-1) until that call and all of the recursive calls made return. This function is structurally comparable to the one you showed and it might help you visualize a little better what's going on.

这篇关于递归思考 Python 2 练习 5.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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