python按引用传递

python按引用传递,python,parameter-passing,depth-first-search,Python,Parameter Passing,Depth First Search,我知道Python是通过“通过引用传递”工作的。然而,当我被困在一个有几个小时的虫子里时,我觉得我仍然不明白事情是怎么发生的。下面是我的代码,它使用深度优先搜索来实现列出集合的所有子集 def subset_of_unique_dfs(mylist): """ :param list: a list of char first assuming the element in the list is unique :return: list of subset of the input list

我知道Python是通过“通过引用传递”工作的。然而,当我被困在一个有几个小时的虫子里时,我觉得我仍然不明白事情是怎么发生的。下面是我的代码,它使用深度优先搜索来实现列出集合的所有子集

def subset_of_unique_dfs(mylist):
"""
:param list: a list of char first assuming the element in the list is unique
:return: list of subset of the input list
we could treated as tree, with ith level represent the ith element is selected or not (binary tree)
when we reach the leafnode of the tree, append the result
so we need to track the level of the tree where we currently at
"""
result_list = []
def helper(thelist, level, curList,result):
    print("at level ",level, "curResult is ",curList)
    if level == len(thelist):
        print type(curList)
        result.append(curList)
        return
    curList.append(thelist[level])
    helper(thelist,level + 1,curList,result)
    curList.pop()
    helper(thelist,level + 1,curList,result)

helper(mylist, 0, [],result_list)
return result_list

print subset_of_unique_dfs(['a','b','c'])
"""
[[], [], [], [], [], [], [], []]
"""
我已经挣扎了一段时间,但一个空的列表被返回。然后我尝试将“result.append(curList)”更改为“result.append(list(curList))”,返回正确的结果。我可以问一下应用列表(卷发器)发生了什么事吗?我过去常常将result.append(curList)作为将与curList绑定的对象附加到result的方式。例如,下面的测试用例实际上显示了append(listA)和append(list(listA))之间没有区别

列表(tmp2)的输出与测试用例中tmp2的输出相同,其工作原理如下:

tmp1 = [[1,2],[2,3]]
tmp1_1 = [[1,2],[2,3]]
tmp2 = [3,4]
tmp1.append(tmp2)
tmp1_1.append([tmp2])
print tmp1
print tmp1_1
"""
[[1, 2], [2, 3], [3, 4]]
[[1, 2], [2, 3], [[3, 4]]]
"""
列表(tmp2)的输出与测试用例中tmp2的输出相同,其工作原理如下:

tmp1 = [[1,2],[2,3]]
tmp1_1 = [[1,2],[2,3]]
tmp2 = [3,4]
tmp1.append(tmp2)
tmp1_1.append([tmp2])
print tmp1
print tmp1_1
"""
[[1, 2], [2, 3], [3, 4]]
[[1, 2], [2, 3], [[3, 4]]]
"""

Python不是“按引用传递”(也不是“按值传递”),只要您认为它是,您就不会理解它“Pass by object”也许是最好的描述。我发现了一个类似的问题(),但仍然有点难以理解“因此,您将解决方案构建为一个列表,其中包含对同一路径的许多引用,最终将其缩减为零”。实际上,我认为它是对不同对象(路径)的相同引用,因为它已经通过append和pop进行了修改……我甚至对“简化为零”感到更困惑。()@TerryJanReedy:“nor'pass by value'”Python中传递和赋值的语义与Java中的相同,Java在这个网站上被普遍称为pass by value。在这里,call by value、call by reference和call by sharing部分解释了“pass by value”如何误导Java对象。Python是按共享调用(或按对象调用或对象共享)。Java有未绑定的值,Python没有,我假设这些Java值是按通常理解的值传递的,严格来说,是非引用的。Fortran、C和传统的值/引用二分法比Java早几十年。Java使用“value”表示引用,这对大多数Python初学者来说是一种误导。@evehsu他们实际上完全不同。分配
tmp2[0]=333
并再次打印
tmp1
print tmp1_1
。Python不是“按引用传递”(也不是“按值传递”),只要你认为它是,你就不会理解它“Pass by object”也许是最好的描述。我发现了一个类似的问题(),但仍然有点难以理解“因此,您将解决方案构建为一个列表,其中包含对同一路径的许多引用,最终将其缩减为零”。实际上,我认为它是对不同对象(路径)的相同引用,因为它已经通过append和pop进行了修改……我甚至对“简化为零”感到更困惑。()@TerryJanReedy:“nor'pass by value'”Python中传递和赋值的语义与Java中的相同,Java在这个网站上被普遍称为pass by value。在这里,call by value、call by reference和call by sharing部分解释了“pass by value”如何误导Java对象。Python是按共享调用(或按对象调用或对象共享)。Java有未绑定的值,Python没有,我假设这些Java值是按通常理解的值传递的,严格来说,是非引用的。Fortran、C和传统的值/引用二分法比Java早几十年。Java使用“value”表示引用,这对大多数Python初学者来说是一种误导。@evehsu他们实际上完全不同。分配
tmp2[0]=333
并再次打印
tmp1
打印tmp1\u 1
。谢谢您的回答。你能帮我进一步澄清tmp1.append(tmp2)实际上是将绑定的对象附加到tmp2的名称,而不是附加tmp2的引用,但是为什么返回到我的函数时,result.append(curList)只附加引用?谢谢你的回答。您是否可以帮助我进一步澄清tmp1.append(tmp2)实际上是将绑定的对象附加到tmp2的名称,而不是附加tmp2的引用,但为什么返回到我的函数时,result.append(curList)只会附加引用?