python的变量就像一个标签
,就一个名字
而已,对变量赋值
就像把名字贴在值上面
>>> a = 3
>>> b = a
>>> c = b
>>> print(id(a),id(b),id(c))
507098848 507098848 507098848
>>> c = 6
>>> print(id(a),id(b),id(c))
507098848 507098848 507098944
>>> list1=[1,2]
>>> list2=list1
>>> list3=list2
>>> print(id(list1),id(list2),id(list3))
50892424 50892424 50892424
>>> list2.append(3)
>>> print(id(list1),id(list2),id(list3))
50892424 50892424 50892424
>>>
>>> print(a,b,c)
3 3 6
>>> print(list1,list2,list3)
[1, 2, 3] [1, 2, 3] [1, 2, 3]
>>>
画图说明:
image.png
还需要说明的是,如果值相同字符串、列表、浮点型、类都是新开辟一块空间,而int类型如果值相同就是指向同一个地址
>>> a="hello world"
>>> b="hello world"
>>> print(id(a),id(b))
52433648 42100016
>>> a=26
>>> b=26
>>> print(id(a),id(b))
507099584 507099584
>>> a=[1,2,3,4,5]
>>> b=[1,2,3,4,5]
>>> print(id(a),id(b))
51856264 52274888
>>> a=int(13)
>>> b=int(13)
>>> print(id(a),id(b))
507099168 507099168
>>> a=0.001
>>> b=0.001
>>> print(id(a),id(b))
34313536 34313704
>>> class C:
pass
>>> c1=C()
>>> c2=C()
>>> print(id(c1),id(c2))
52430888 52301664
>>> class C:
def __init__(self,num):
self.value = num
>>> a=C(26)
>>> b=C(26)
>>> print(id(a),id(b))
52431112 52431056
网友评论