美文网首页
python赋值机制

python赋值机制

作者: 脏脏包盛 | 来源:发表于2017-12-17 11:43 被阅读0次
x = [1, 2, 3]
y = x
x[1] = 100
print(y)

[1, 100, 3]
改变变量x的值,变量y的值也随着改变,这与Python内部的赋值机制有关。

简单机制

先来看这一段代码在Python中的执行过程。

x = 500
y = x
y = 'foo'
  • x = 500
    Python分配了一个 PyInt 大小的内存 pos1 用来储存对象 500 ,然后,Python在命名空间中让变量 x 指向了这一块内存,注意,整数是不可变类型,所以这块内存的内容是不可变的。
内存 命名空间
pos1 : PyInt(500) (不可变) x : pos1
  • y = x
内存 命名空间
pos1 : PyInt(500) (不可变) x : pos1 y : pos1
  • y = 'foo'
    Python此时分配一个 PyStr 大小的内存 pos2 来储存对象 foo ,然后改变变量 y 所指的对象。
内存 命名空间
pos1 : PyInt(500) (不可变) x : pos1
pos2 : PyStr('foo') (不可变) y : pos2

对这一过程进行验证,可以使用 id 函数。

id(x)

返回变量 x 的内存地址。

x = 500
id(x)

48220272L

y = x
id(y)

48220272L
也可以使用 is 来判断是不是指向同一个事物:

x is y

True
现在 y 指向另一块内存:

y = 'foo'
id(y)

39148320L

x is y

False

Python会为每个出现的对象进行赋值,哪怕它们的值是一样的,例如:

x = 500
id(x)

48220296L

y = 500
id(y)

48220224L

x is y

False
不过,为了提高内存利用效率,对于一些简单的对象,如一些数值较小的int对象,Python采用了重用对象内存的办法:

x = 2
id(x)

6579504L

y = 2
id(y)

6579504L

x is y

True

容器类型

现在来看另一段代码:

x = [500, 501, 502]
y = x
y[1] = 600
y = [700, 800]
  • x = [500, 501, 502]
    Python为3个PyInt分配内存 pos1 , pos2 , pos3 (不可变),然后为列表分配一段内存 pos4 ,它包含3个位置,分别指向这3个内存,最后再让变量 x 指向这个列表。
内存 命名空间
pos1 : PyInt(500) (不可变)
pos2 : PyInt(501) (不可变)
pos3 : PyInt(502) (不可变)
pos4 : PyList(pos1, pos2, pos3) (可变) x : pos4
  • y = x
    并没有创建新的对象,只需要将 y 指向 pos4 即可。
内存 命名空间
pos1 : PyInt(500) (不可变)
pos2 : PyInt(501) (不可变)
pos3 : PyInt(502) (不可变) x : pos4
pos4 : PyList(pos1, pos2, pos3) (可变) y: pos4
  • y[1] = 600
    原来 y[1] 这个位置指向的是 pos2 ,由于不能修改 pos2 的值,所以首先为 600 分配新内存 pos5 。
    再把 y[1] 指向的位置修改为 pos5 。此时,由于 pos2 位置的对象已经没有用了,Python会自动调用垃圾处理机制将它回收。
内存 命名空间
pos1 : PyInt(500)(不可变)
pos2 : 垃圾回收
pos3 : PyInt(502) (不可变)
pos4 : PyList(pos1, pos5, pos3) (可变) x : pos4
pos5 : PyInt(600) (不可变) y: pos4
  • y = [700, 800]
内存 命名空间
pos1 : PyInt(500) (不可变)
pos3 : PyInt(502) (不可变)
pos4 : PyList(pos1, pos5, pos3) (可变)
pos5 : PyInt(600) (不可变)
pos6 : PyInt(700) (不可变)
pos7 : PyInt(800) (不可变) x : pos4
pos8 : PyList(pos6, pos7) (可变) y : pos8

对这一过程进行验证:

x = [500, 501, 502]
print( id(x[0]))
print (id(x[1]))
print (id(x[2]))
print (id(x))

48220224
48220248
48220200
54993032

赋值,id(y) 与 id(x) 相同。

y = x
print(id(y))

54993032

x is y

True

修改 y[1] ,id(y) 并不改变。

y[1] = 600
print (id(y))

54993032

id(x[1]) 和 id(y[1]) 的值改变了。

print(id(x[1]))
print(id(y[1]))

48220272
48220272

更改 y 的值,id(y) 的值改变

y = [700, 800]
print(id(y))
print(id(x))

54995272
54993032

相关文章

  • Python 赋值机制

  • python赋值机制

    [1, 100, 3]改变变量x的值,变量y的值也随着改变,这与Python内部的赋值机制有关。 简单机制 先来看...

  • python赋值机制

    对应于元组(tuple)与列表(list)的关系,对于集合(set),Python提供了一种叫做不可变集合(fro...

  • python的赋值机制

    python的一切是对象 不只是变量是对象,python的一切都是对象 这个代码会做三件事情 1,生成一个int对...

  • 2018-09-14day20-内存管理机制

    掌握:1.数字、字符串、布尔的缓存2.垃圾回收机制中的引用计数机制 1.python中变量的赋值 python中所...

  • 2018-09-14 Day20-内存管理机制

    一、python中变量的赋值 掌握:(1)、数字、字符串、布尔值的缓存(2)、垃圾回收机制中的引用计数机制 pyt...

  • 对于新手来说,Python 中有哪些难以理解的概念?

    老手都是从新手一路过来的,提起Python中难以理解的概念,可能很多人对于Python变量赋值的机制有些疑惑,不过...

  • Python 防止变量被隐式改变

    相关知识 Python 的变量类型 Python 的赋值机制 同级变量隐式改变 当两个变量指向同一个可变实例时,一...

  • Python变量和数据类型

    一. Python中的变量赋值 单变量赋值: 多变量赋值: 二. Python中的数据类型 Python有五个...

  • python-List

    python赋初值 python 全部清零 python赋值是引用赋值,无引用就回收python与Java一样,不...

网友评论

      本文标题:python赋值机制

      本文链接:https://www.haomeiwen.com/subject/ujsywxtx.html