简体   繁体   English

是否在 Python 中复制了返回类型?

[英]Are return types copied in Python?

I am trying to understand how return works in Python and whether a move operation is being used behind the scenes.我试图了解 return 在 Python 中的工作原理以及是否在幕后使用了移动操作。 Consider the following code:考虑以下代码:

def get_graph(maze): # maze is a 2D bool matrix, with True representing walls
    nrow = len(maze)
    ncol = len(maze[0])
    graph = {}
    for row in range(nrow):
        for col in range(ncol):
            edges = []
            if col - 1 != -1 and maze[row][col-1] is False:
                edges.append((row, col-1))  # left_edge
            if col + 1 != ncol and maze[row][col+1] is False:
                edges.append((row, col+1))  # right_edge
            if row - 1 != - 1 and maze[row-1][col] is False:
                edges.append((row-1, col))  # top_edge
            if row + 1 != nrow and maze[row+1][col] is False:
                edges.append((row+1, col))  # bottom_edge
            graph[(row, col)] = edges
    return graph

Would graph be moved to g in below code? graph会在下面的代码中移动到g吗? I assume no deep copy is being performed here.我假设这里没有执行深层复制。

mat=[[False, False, False, False],
       [True, True, False, True],
       [False, False, False, False],
       [False, False, False, False]]

g = get_graph(mat)

Python has no concept of memory locations or the 'moving' of data. Python 没有 memory 位置或数据“移动”的概念。

It has two concepts: objects and names.它有两个概念:对象和名称。

As far as a user of Python is considered, objects live in a magic cloud, automatically delete themselves when no longer necessary and never move.就 Python 的用户而言,对象生活在一个神奇的云中,当不再需要时会自动删除自己并且永远不会移动。

Names are created by the assignment operator.名称由赋值运算符创建。 When I say a = 5 , that does not mean that now a 'contains' the object 5 or that it has 'moved to a', it means I simply created a new name a for the object on the right hand side, which is 5.当我说a = 5时,这并不意味着现在a '包含' object 5 或者它已经'移动到 a',这意味着我只是为右侧的 object 创建了一个新名称a ,即5.

Similarly, g = f() for arbitrary function f takes whatever object f returned and makes g a name for that object.类似地,对于任意 function fg = f()采用返回的任何 object f并将g命名为该 object 的名称。 It does not copy or move anything.它不会复制或移动任何东西。

You seem to be thinking of variables as containers of data, when they are better thought of as references to data, so that the object doesn't get moved so much as a reference to that object gets copied, hopefully making clear why no copy (deep or otherwise) is involved.您似乎将变量视为数据的容器,而最好将它们视为对数据的引用,因此object不会像引用 object 那样被复制,希望能弄清楚为什么没有复制(深或其他)涉及。

Do an experiment: If you put print(id(graph)) immediately before the return and put print(id(g)) immediately after the assignment, the same id will be printed, showing that it is only a reference which is returned.做一个实验:如果你把print(id(graph))放在 return 前面,把print(id(g))放在后面紧跟赋值,同样的id会被打印出来,说明它只是一个返回的引用。 This returned reference will prevent the graph from being garbage-collected, which is the usual fate of local variables after a function return.这个返回的引用将防止图形被垃圾收集,这是 function 返回后局部变量的通常命运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM