简体   繁体   English

无法理解python中的可变对象

[英]Can't understand mutable objects in python

So I thought that lists were MUTABLE objects in python so they could be changed. 所以我认为列表是python中的MUTABLE对象,因此可以更改它们。

When I create a list: 创建列表时:

list = [1, 2, 3]
print(id(list))
list = [4, 5, 6]
print(id(list))

I should get the same ID in both cases.. But I got different one. 在两种情况下,我都应该获得相同的ID。但是我得到的是不同的ID。 Why is that? 这是为什么?

The operator = with just a name on the left hand side always assigns an object to the name. 仅在左侧具有名称的运算符=始终为该名称分配一个对象。 It does not do anything else. 它没有做任何其他事情。

The operation = with a name that has an index (something in brackets) calls __setitem__ on the named object. 名称=带有索引(括号中的内容)的操作=调用命名对象上的__setitem__

You are invoking the first behavior. 您正在调用第一个行为。 You are creating a new list, discarding the old one, and assigning the new one to the name list . 您正在创建一个新列表,丢弃旧列表,并将新列表分配给名称list

To mutate the object instead of replacing, you'll want to invoke the second behavior: 要对对象进行突变而不是替换,您将需要调用第二种行为:

lst[:] = [4, 5, 6]

This assigns the new values to the elements of the original list. 这会将新值分配给原始列表的元素 Now the contents of the original list will change, but it will be the same object with the same id, as you expected. 现在,原始列表的内容将更改,但是与您期望的一样,它将是具有相同ID的相同对象。

In both cases, a new list object is created any time you put a comma-separated list in square brackets. 在这两种情况下,只要将方括号括起来的逗号分隔列表都将创建一个新的列表对象。 [1, 2, 3] creates a list and so does [4, 5, 6] . [1, 2, 3]创建一个列表, [4, 5, 6] The difference is in what you do with the second list. 区别在于您对第二个列表的处理方式。 lst = [4, 5, 6] assigns it to the name lst , discarding any previous object that name may have been bound to. lst = [4, 5, 6]将其分配给名称lst ,丢弃该名称可能已绑定到的所有先前对象。 lst[:] = [4, 5, 6] is actually roughly equivalent to doing lst.__setitem__(slice (None), [4, 5, 6]) . lst[:] = [4, 5, 6]实际上大致等同于执行lst.__setitem__(slice (None), [4, 5, 6]) This copies the elements of the second list into the first, but doesn't alter any name bindings. 这会将第二个列表的元素复制到第一个列表中,但不会更改任何名称绑定。

And don't call a variable list . 并且不要调用变量list It shadows a builtin function. 它隐藏了内置函数。 There's nothing fundamentally to prevent you from doing this, it's just rebinding a new object to an existing name (like your original example). 从根本上没有什么可以阻止您执行此操作,只是将新对象重新绑定到现有名称(如您的原始示例)。 But then you'll want to use the list function, and join the hordes on Stack Overflow always asking why their builtins don't work. 但是随后您将要使用list函数,并在Stack Overflow上加入成群结队,总是问为什么内置函数不起作用。

In the second line, you are just creating a new list object and assigning it to list. 在第二行中,您只是在创建一个新的列表对象并将其分配给列表。

list=[4,5,6] //creates a new list object and puts its reference in list

To show that list are mutable, you can try applying operations on this line: 为了显示该列表是可变的,您可以尝试在此行应用操作:

list=[1,2,3]

Like: 喜欢:

>>> a = [1,2,3]
>>> id(a)
2814816838216
>>> a[0] = 4
>>> a
[4, 2, 3]
>>> id(a)
2814816838216

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

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