[英]Recursive function, final print statement
I'm self-learning Python, using How to Think Like a Computer Scientist. 我正在使用如何像计算机科学家一样思考的方式自学Python。 I am learning "Nodes and Linked Lists".
我正在学习“节点和链接列表”。 This recursive function is confusing me.
这个递归函数使我感到困惑。 To be clear, the code works fine.
需要明确的是,该代码可以正常工作。 I am asking, how is it possible that the final line of code at the bottom (
print list,
) is being executed? 我问的是,底部的最后一行代码(
print list,
)如何被执行?
My question is regarding this recursive function: 我的问题是关于此递归函数的:
def print_backward (list):
if list == None: return #shouldn't the code terminate when this is satisfied?
print_backward(list.next)
print list, #seems like the function would terminate before this line
#ever gets called
Can someone explain to me, when is this last line print head,
being executed? 有人可以告诉我,这最后一行
print head,
何时执行? Reading this code, I would think that we loop back to the top of the function after evaluating each node, then when we reach the 3rd and final node, the terminating statement if list == None: return
will be satisfied, and the code would then exit the loop, never reaching the print statement at the very bottom. 阅读这段代码,我认为我们在评估完每个节点后会回到函数的顶部,然后当我们到达第三个节点和最后一个节点时,
if list == None: return
,则终止语句if list == None: return
将被满足,并且代码将然后退出循环,从不到达最底部的打印语句。 Clearly that is not happening though, since the print statement IS being called. 显然,这不会发生,因为正在调用print语句。
I'm asking because I feel like I'm not really learning if I don't understand how the code works! 我之所以问是因为,如果我不了解代码的工作原理,那感觉就像是我不是在真正地学习! If someone can explain how the code gets to that final print statement (and it is getting there), I'd really appreciate it.
如果有人可以解释代码如何到达最终的打印语句(并且它已经到达那里),我将非常感激。 I hope I'm not violating the rules by asking this.
我希望我不要通过问这个来违反规则。 Thanks!
谢谢!
BTW I'm printing a linked, list, cargo is [1,2,3]. 顺便说一句,我正在打印一个链接清单,货物是[1,2,3]。 Printing it backward, so [3,2,1] Below, is the code for more context.
向后打印,因此下面的[3,2,1]是更多上下文的代码。
class Node:
def __init__(self, cargo = None, next = None):
self.cargo = cargo
self.next = next
def __str__(self):
return str(self.cargo)
def print_list(node):
while node:
print node,
node = node.next
print
def print_backward (list):
if list == None: return
print_backward(list.next)
print list,
node1 = Node(1)
node2 = Node(2)
node3 = Node(3)
node1.next = node2
node2.next = node3
node3.next = None
print_backward(node1)
Output is: 输出为:
==== RESTART: /Users/Desktop/Programming Career/Untitled.py ====
3 2 1
Let's parse through what the code is doing node by node 让我们逐个代码分析代码在做什么
print_backward
is called with node1
node1
调用print_backward
node1 == None
none? node1 == None
无? No so we continue head = node1
head = node1
tail = node1.next
same as tail = node2
tail = node1.next
与tail = node2
相同 print_backward(tail)
same as print_backward(node2)
print_backward(tail)
与print_backward(node2)
相同
print_backward
is called with node2
node2
调用print_backward
node2 == None
none? node2 == None
无? No so we continue head = node2
head = node2
tail = node2.next
same as tail = node3
tail = node2.next
与tail = node3
相同 print_backward(tail)
same as print_backward(node3)
print_backward(tail)
与print_backward(node3)
相同
print_backward
is called with node3
node3
调用print_backward
node3 == None
none? node3 == None
无? No so we continue head = node3
head = node3
tail = node3.next
same as tail = None
tail = node3.next
与tail = None
相同 print_backward(tail)
same as print_backward(None)
print_backward(tail)
与print_backward(None)
相同
print_backward
is called with None
None
调用print_backward
None == None
none? None == None
? Yes so we return print head,
is called, same as print node3,
(printing "3 ") print node3,
3相同print head,
称为print head,
(打印“ 3”) print head,
is called, same as print node2,
(printing "2 ") print node2,
2相同print head,
称为print head,
(打印“ 2”) print head,
is called, same as print node1,
(printing "1 ") print node1,
1相同print head,
称为print head,
(打印“ 1”) Total output is, "3 2 1 "! 总输出为“ 3 2 1”!
Recursion doesn't mean jumping to the top of the function; 递归并不意味着跳到函数的顶部; it means calling the same function within a function.
这意味着在一个函数内调用相同的函数。
I think a simpler example makes it more obvious: 我认为一个更简单的示例将使其更加明显:
def count(number):
if number <= 0: return
count(number-1)
print(number)
When we call count(3)
, the function will print the numbers 1 through 3. Why? 当我们调用
count(3)
,该函数将打印数字1到3。为什么? Because this is what happens: 因为这是发生的情况:
count(3)
is called. count(3)
被调用。 number
is 3, which is not below or equal to zero, so we don't return
. number
是3,不小于或等于零,因此我们不return
。 count(3-1)
: count(3-1)
:
count(2)
is called. count(2)
被调用。 number
is 2, which is not below or equal to zero, so we don't return
. number
为2,不小于或等于零,因此我们不return
。 count(2-1)
: count(2-1)
:
count(1)
is called. count(1)
被调用。 number
is 1, which is not below or equal to zero, so we don't return
. number
为1,不小于或等于零,因此我们不return
。 count(1-1)
: count(1-1)
:
count(0)
is called. count(0)
被调用。 number
is 0, which is equal to zero, so we return
to the previous level. number
为0,等于零,因此我们return
上一级。 print
1. print
1。 print
2. print
2。 print
3. print
3。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.