Python基础——赋值机制

使用id()函数用于获取对象的内存地址。

使用is来判断是不是指向同一个内存。

把一个对象赋值给另一个对象,两个对象都指向同一个内存地址。

test=1000
test1=test
id(test)
id(test1)
test is test1

两个不同的对象指向不同的内存地址。

test=1000
test1=123456
id(test)
id(test1)
test is test1

分别给两个对象赋值,当数值小的时候,指向同一个内存地址;数值大的时候,指向不同的内存地址。

test=1000
test1=1000
id(test)
id(test1)
test=1
test1=1
id(test)
id(test1)

猜你喜欢

转载自www.cnblogs.com/gloria-zhang/p/10571881.html