Understanding mutability in Python [closed]

梦想与她 提交于 2019-12-18 09:25:40

问题


I have this snippet:

a = [1,2,3]
b = a
b = [4,5,6] # a doesnt change

and this:

a = [1,2,3]
b = a
b[0] = 5 # a also changes

How is b's initialization playing a part in deciding the mutability of a?


回答1:


When you create a list and assign it to a variable, like

a = [1, 2, 3]

You create an object, and the variable a holds a reference to that object. When you do

b = a

You assign the same reference in b, so now both a and b point to your list. So when you execute

b[0] = 5

You alter that same list.

You can see this in action by using the id() function, which returns the memory address of the object:

>>> a = [1, 2, 3]
>>> b = a
>>> id(a), id(b)
(140454191340720, 140454191340720)

They are identical. The point is, a and b are not lists themselves, they point to a list.

When you do something like:

a = [1, 2, 3]
b = a
b = [2, 3, 4]

You first assigned to b a reference to the list that a points to, but then you assign a new reference to it.

By the way, this can bite you in the behind when you do something along the lines of

def foo (a=[]):
    a.append(42)
    return a

since the argument a points to the same list on every invocation, if you call this function 5 times without arguments, the list will contain 5x 42:

foo(); foo(); foo(); foo();
print(foo())
>>> [42, 42, 42, 42]


来源:https://stackoverflow.com/questions/16731404/understanding-mutability-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!