变量,id()

时间:2021-11-27 19:05:53
 >>> a = 1
>>> print id(a)
2870961640
>>> b = a
>>> print id(b)
2870961640
>>> c = 1
>>> print id(c)
2870961640
>>>

在Python中,整数和短小的字符,Python都会缓存这些对象,以便重复使用。当我们创建多个等于1的引用时,实际上是让所有这些引用指向同一个对象。

 # True
a = 1
b = 1
print(a is b) # True
a = "good"
b = "good"
print(a is b) # False
a = "very good morning"
b = "very good morning"
print(a is b) # False
a = []
b = []
print(a is b)