为什么在Python中变量有时会绑定两个方向?

为什么在Python中变量有时会绑定两个方向?,python,python-3.x,variables,binding,Python,Python 3.x,Variables,Binding,说我有 x = [[0,0]] y = x[-1] 那为什么呢 y[1] += 1 给予 这是,我困惑了为什么它也改变了X是什么,即使我只与Y有什么关系?< p>这是有意义的,如果你把一个列表看作是一个在内存中可变的对象,你认为你正在修改那个列表。 变量只是同一列表的不同名称 该代码相当于: list_obj = [0, 0] x = [ list_obj ] # a list with list_obj as # its

说我有

    x = [[0,0]]
    y = x[-1]
那为什么呢

    y[1] += 1
给予


这是,我困惑了为什么它也改变了X是什么,即使我只与Y有什么关系?

< p>这是有意义的,如果你把一个列表看作是一个在内存中可变的对象,你认为你正在修改那个列表。 变量只是同一列表的不同名称

该代码相当于:

list_obj = [0, 0]
x = [ list_obj ]    # a list with list_obj as
                    # its single element
y = list_obj        # same as x[-1], just
                    # another name for list_obj

在这两种情况下,您只需修改
list_obj[1]

为此,您需要研究python*中对象的*引用(谷歌搜索)?
y
指向同一个对象。
list_obj = [0, 0]
x = [ list_obj ]    # a list with list_obj as
                    # its single element
y = list_obj        # same as x[-1], just
                    # another name for list_obj