Python - 在递归函数中使用共享变量

Ali*_*itt 8 python recursion global-variables return-value

我正在使用递归函数对Python中的列表进行排序,并且我希望在函数继续时跟踪排序/合并的数量.但是,当我在函数内部声明/初始化变量时,它会在函数的每次连续调用中成为局部变量.如果我在函数外部声明变量,则函数认为它不存在(即无法访问它).如何在函数的不同调用之间共享此值?

我试图在函数内外使用"全局"变量标签,如下所示:

global invcount  ## I tried here, with and without the global tag

def inv_sort (listIn):
    global invcount   ## and here, with and without the global tag

    if (invcount == undefined):  ## can't figure this part out
        invcount = 0

    #do stuff
Run Code Online (Sandbox Code Playgroud)

但我无法弄清楚如何检查全局变量的未定义状态,并在第一次递归调用时给它一个值(因为在所有连续的递归中它应该有一个值并被定义).

我的第一个想法是从函数的每次调用中返回变量,但是我无法弄清楚如何从函数中传递两个对象,并且我已经必须将列表传递给递归排序才能工作.我第二次尝试解决这个问题涉及到我将变量添加invcount到我传递的列表作为带有标识符的最后一个元素,比如"i27".然后我可以检查i最后一个元素中是否存在标识符(本例中的字母),如果存在pop(),则在函数调用开始时将其关闭,并在递归期间重新添加它.在实践中,这变得非常复杂,虽然最终可能有效,但我想知道是否有更实用或更简单的解决方案.

有没有办法在不直接传递/返回变量的情况下共享变量?

Dan*_*man 7

Python 中不存在“未定义”变量这样的东西,而且您也不需要它。

在函数外部,将变量设置为 0。在循环内部,使用global关键字,然后递增。

invcount = 0
def inv_sort (listIn):
    global invcount

    ... do stuff ...

    invcount += 1
Run Code Online (Sandbox Code Playgroud)


Ale*_*kop 7

你可以做几件事.举个例子你应该像这样修改它:

invcount = 0

def inv_sort (listIn):
    global invcount

    invcount += 1

    # do stuff
Run Code Online (Sandbox Code Playgroud)

但这种方法意味着你应该invcount在每次调用之前归零inv_sort.所以实际上最好invcount作为结果的一部分返回.例如,使用这样的元组:

def inv_sort(listIn):

    #somewhere in your code recursive call
    recursive_result, recursive_invcount = inv_sort(argument)

    # this_call_invcount includes recursive_invcount
    return this_call_result, this_call_invcount   
Run Code Online (Sandbox Code Playgroud)


jon*_*rpe 5

替代方案可能是使用默认参数,例如:

def inv_sort(listIn, invcount=0):
    ...
    invcount += 1
    ...
    listIn, invcount = inv_sort(listIn, invcount)        
    ...
    return listIn, invcount
Run Code Online (Sandbox Code Playgroud)

这样做的缺点是你的电话会变得不那么整洁:

l, _ = inv_sort(l) # i.e. ignore the second returned parameter
Run Code Online (Sandbox Code Playgroud)

但这确实意味着invcount每次使用单个参数调用函数时自动重置(并且还提供了invcount在测试时注入值的机会:) assert result, 6 == inv_sort(test, 5).