我认为您对递归有些困惑。递归函数是调用自身的函数。您的示例函数改为调用它的参数f
,这意味着它只有在将自身传递为 时才是递归的f
。
这是一个真正的递归函数的样子:
def recursive(arg):
if arg <= 0:
return "base case"
else:
return "recursive({}) returned <{}>".format(arg-1, recursive(arg-1))
示例输出:
>>> recursive(0)
'base case'
>>> recursive(3)
'recursive(2) returned <recursive(1) returned <recursive(0) returned <base case>>>'
正如您在该示例中所看到的,您总是需要有一个函数不递归的基本情况,否则您将永远不会到达终点。
通过修改在每个调用中传递的参数,可以将信息“向上”传递到递归调用链。通过修改递归调用的返回值,信息可以“向下”传递,以创建您自己的返回值。
一般来说,函数调用永远不会修改调用函数中的局部变量(有几种方法可以,但并不常见)。对于递归调用,这意味着函数的每个调用都有自己的每个局部变量版本。函数参数是局部变量,因此它们对于每个调用也是唯一的(并且可以相互独立地修改)。
def recursive_vars(arg):
loc = 10 # a local variable
print("initial values of local variables are: arg = {}, loc = {}".format(arg, loc))
if arg == 0:
print("arg is zero, so this is the base case. Returning without recusing!")
return
print("decrementing arg and loc by one each")
arg -= 1
loc -= 1
print("before recursion, local variables are: arg = {}, loc = {}".format(arg, loc))
print("recursing")
recursive_vars(arg)
print("after recursion, local variables are: arg = {}, loc = {}".format(arg, loc))
print("done")
输出:
>>> recursive_vars(0)
initial values of local variables are: arg = 0, loc = 10
arg is zero, so this is the base case. Returning without recusing!
>>> recursive_vars(3)
initial values of local variables are: arg = 3, loc = 10
decrementing arg and loc by one each
before recursion, local variables are: arg = 2, loc = 9
recursing
initial values of local variables are: arg = 2, loc = 10
decrementing arg and loc by one each
before recursion, local variables are: arg = 1, loc = 9
recursing
initial values of local variables are: arg = 1, loc = 10
decrementing arg and loc by one each
before recursion, local variables are: arg = 0, loc = 9
recursing
initial values of local variables are: arg = 0, loc = 10
arg is zero, so this is the base case. Returning without recusing!
after recursion, local variables are: arg = 0, loc = 9
done
after recursion, local variables are: arg = 1, loc = 9
done
after recursion, local variables are: arg = 2, loc = 9
done
如果根据递归的深度进行缩进,则输出的最后一部分如下所示:
initial values of local variables are: arg = 3, loc = 10
decrementing arg and loc by one each
before recursion, local variables are: arg = 2, loc = 9
recursing
initial values of local variables are: arg = 2, loc = 10
decrementing arg and loc by one each
before recursion, local variables are: arg = 1, loc = 9
recursing
initial values of local variables are: arg = 1, loc = 10
decrementing arg and loc by one each
before recursion, local variables are: arg = 0, loc = 9
recursing
initial values of local variables are: arg = 0, loc = 10
arg is zero, so this is the base case. Returning without recusing!
after recursion, local variables are: arg = 0, loc = 9
done
after recursion, local variables are: arg = 1, loc = 9
done
after recursion, local variables are: arg = 2, loc = 9
done
如您所见,在每种情况下,每一层中的局部变量在递归调用的两侧都具有相同的值。因为arg
变量是作为参数传递的,所以看起来像是在调用之间共享,但这是一种错觉。正如您所看到的,当函数调用展开时,外部调用的arg
变量值没有被内部调用修改。(如果您传递可变对象,例如作为参数的实例,事情会稍微复杂list
一些,但这对于递归的基本理解并不重要。)