Python-range()是否在递归中变异变量值?
在理解字符串排列及其在python中的实现(关于)时,我在使用Python-range()是否在递归中变异变量值?,python,recursion,Python,Recursion,在理解字符串排列及其在python中的实现(关于)时,我在使用range()的for循环中偶然发现了一些东西 以下面的代码为例: def recursion(step=0): print "Step I: {}".format(step) for i in range(step, 2): print "Step II: {}".format(step) print "Value i: {}".format(i) print "Ca
range()
的for
循环中偶然发现了一些东西
以下面的代码为例:
def recursion(step=0):
print "Step I: {}".format(step)
for i in range(step, 2):
print "Step II: {}".format(step)
print "Value i: {}".format(i)
print "Call recursion"
print "\n-----------------\n"
recursion(step + 1)
recursion()
这将产生以下输出:
root@host:~# python range_test.py
Step I: 0
Step II: 0
Value i: 0
Call recursion
-----------------
Step I: 1
Step II: 1
Value i: 1
Call recursion
-----------------
Step I: 2
Step II: 0 <---- WHAT THE HECK?
Value i: 1
Call recursion
-----------------
Step I: 1
Step II: 1
Value i: 1
Call recursion
-----------------
Step I: 2
root@host:~#
root@host:~#python range_test.py
第一步:0
第二步:0
第一值:0
调用递归
-----------------
第一步:1
第二步:1
价值一:1
调用递归
-----------------
第一步:2
第二步:0你的结论是不正确的<代码>步骤
值不会通过使用范围
更改。
这可以通过以下方式进行验证:
def no_recursion(step=0):
print "Step I: {}".format(step)
for i in range(step, 2):
print "Step II: {}".format(step)
print "Value i: {}".format(i)
no_recursion(step=2)
它产生输出:
Step I: 2
def recursion(step=0):
print "recursion called with [step = {}]".format(step) # add entry logging
print "Step I: {}".format(step)
for i in range(step, 2):
print "Step II: {}".format(step)
print "Value i: {}".format(i)
print "Call recursion"
print "\n-----------------\n"
recursion(step + 1)
print "--> returned recursion [step = {}]".format(step) # add exit logging
recursion()
这是预期的,因为范围(2,2)
返回[]
step
将其值更改为0的错觉是因为函数递归(使用step=2
调用)在打印step I:2
后返回,然后控制返回到函数递归(使用step=1
调用)它立即返回,因为它的for循环
已终止,然后控制返回到递归
(使用step=0
调用),该递归自剩下1次迭代后继续,并将步骤II:0
打印到控制台,这并不奇怪。如果我们稍微修改代码(通过添加函数入口和出口日志记录)并观察输出,这将更容易观察:
Step I: 2
def recursion(step=0):
print "recursion called with [step = {}]".format(step) # add entry logging
print "Step I: {}".format(step)
for i in range(step, 2):
print "Step II: {}".format(step)
print "Value i: {}".format(i)
print "Call recursion"
print "\n-----------------\n"
recursion(step + 1)
print "--> returned recursion [step = {}]".format(step) # add exit logging
recursion()
此代码产生的输出为:
recursion called with [step = 0]
Step I: 0
Step II: 0
Value i: 0
Call recursion
-----------------
recursion called with [step = 1]
Step I: 1
Step II: 1
Value i: 1
Call recursion
-----------------
recursion called with [step = 2]
Step I: 2
--> returned recursion [step = 2]
--> returned recursion [step = 1]
Step II: 0
Value i: 1
Call recursion
-----------------
recursion called with [step = 1]
Step I: 1
Step II: 1
Value i: 1
Call recursion
-----------------
recursion called with [step = 2]
Step I: 2
--> returned recursion [step = 2]
--> returned recursion [step = 1]
--> returned recursion [step = 0]
我们可以清楚地看到递归展开的顺序,并观察到步骤的值在每个步骤中是一致的。范围(2,2)
是空的。它不会为
循环输入。因此,它打印步骤I:2
,然后返回,之前的范围(1,2)
函数也完成了1次迭代,因此您回到了步骤
为0的位置。@Ralfmaroglatt很高兴这有帮助!