我認爲你對遞歸有一些困惑。遞歸函數是一個自我調用的函數。你的示例函數改爲調用它的參數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
實例作爲參數,但這對於遞歸的基本理解並不重要。)
因爲在你函數的一行上你做'p = 1' .. – hetepeperfan
@het:我也有一條直線n + = 1和一條直線p + = 1,但p從未達到2以上,而n達到6以上。 – jason
這是什麼貸款說。 'p'是一個局部變量,'n'相反是一個全局變量。每次函數運行p都將「重置」,「n」,全局不會重置。 – hetepeperfan