变量,id()
1 >>> a = 1 2 >>> print id(a) 3 2870961640 4 >>> b = a 5 >>> print id(b) 6 2870961640 7 >>> c = 1 8 >>> print id(c) 9 2870961640 10 >>>
在Python中,整数和短小的字符,Python都会缓存这些对象,以便重复使用。当我们创建多个等于1的引用时,实际上是让所有这些引用指向同一个对象。
1 # True 2 a = 1 3 b = 1 4 print(a is b) 5 6 # True 7 a = "good" 8 b = "good" 9 print(a is b) 10 11 # False 12 a = "very good morning" 13 b = "very good morning" 14 print(a is b) 15 16 # False 17 a = [] 18 b = [] 19 print(a is b)