table of Contents
- Three elements of objects in Python (Id, Type, Value)
is
VS==
is
Several special casesPython 编译机制
| Code block|小整数对象池
and字符串intern机制
(instructions only, see another article for details)- Reference
Three elements of objects in Python (Id, Type, Value)
-
Id : uniquely identify an object
-
Type : Identifies the type of object
-
Value : the value of the object
>>> my_string = "I am a string" >>> id(my_string) 4498919600 >>> type(my_string) <class 'str'> >>> my_string 'I am a string'
is
VS ==
-
is
In Python, it is called identity operator or identity operator . The comparison judges the unique identity between objects, that is, comparing the Id of the object, that is, comparing whether the positions of the two objects in the memory are the same . -
==
Is a standard comparison operators , comparison determination whether two objects Value equal to default call object__eq__()
methods .>>> a = "I am a string" >>> b = a >>> c = "I am a string" >>> a is b True >>> a is c False >>> a == b True >>> a == c True >>> id(a) 4311719856 >>> id(b) 4311719856 >>> id(c) 4311765936
is
Several special cases
The is comparison judges whether the Ids of different objects are equal.
This standard is applicable when comparing most types of objects, but there are some differences when comparing int and str .
>>> a = (1, 2, 3) # a 和 b 为 tuple 类型
>>> b = (1, 2, 3)
>>> a is b
False
>>> id(a)
4357441024
>>> id(b)
4357526592
>>>
>>> a = [1, 2, 3] # a 和 b 为 list 类型
>>> b = [1, 2, 3]
>>> a is b
False
>>> id(a)
4357525632
>>> id(b)
4357525760
>>>
>>> a = {
1: 10, 2: 20, 3: 30} # a 和 b 为 dict 类型
>>> b = {
1: 10, 2: 20, 3: 30}
>>> a is b
False
>>> id(a)
4357133376
>>> id(b)
4357133440
>>>
>>> a = set([1, 2, 3]) # a 和 b 为 set 类型
>>> b = set([1, 2, 3])
>>> a is b
False
>>> id(a)
4357494592
>>> id(b)
4357495040
>>>
>>> a = 1 # a 和 b 为 int 类型
>>> b = 1
>>> a is b
True
>>> id(a)
4354664896
>>> id(b)
4354664896
>>>
>>> a = "string" # a 和 b 为 str 类型
>>> b = "string"
>>> a is b
True
>>> id(a)
4357527088
>>> id(b)
4357527088
It can be clearly seen that when comparing int and str, although two objects a and b are defined respectively, the result of comparing these two objects with is is True!
How is this going?
This is contrary to the definition of is!
But at the same time, we can also notice that when comparing int and str, the Id of the two objects a and b defined respectively are the same!
- It turns out that in order to optimize speed and avoid frequent application and destruction of memory space for integers , there is such a mechanism in Python:
For integer within [-5, 257) range , Python will be created when the program starts a small integer pool, they are all saved to a named
small_ints
list of this integer pool will be accompanied by the entire life cycle of Python programs, not Will be recycled. Later, if you want to create an integer in the range [-5, 257), Python will directly refer to the corresponding object in the integer pool .Therefore, for the integers in the interval [-5, 257), as long as their values are equal, then their reference addresses are all equal, that is, the Id is equal .
This mechanism is called " small integer object pool " in Python .
>>> a = -5 >>> b = -5 >>> a is b True >>> id(a) 4391184640 >>> id(b) 4391184640 >>> a = 256 >>> b = 256 >>> a is b True >>> id(a) 4391192992 >>> id(b) 4391192992 >>> a = -6 >>> b = -6 >>> a is b False >>> id(a) 4393924016 >>> id(b) 4393923984 >>> a = 257 >>> b = 257 >>> a is b False >>> id(a) 4393924048 >>> id(b) 4393924016
-
For str, there is also a similar mechanism in Python:
In Python, string objects are immutable objects .
Python maintains a dictionary that maintains the created string (key) and the address (value) of the string object that conform to the maintenance mechanism.
Every time a string object is created, it will be compared with the dictionary. If the string conforms to the maintenance mechanism ( only contains [0-9a-zA-Z_] ) and already exists in the dictionary, then the newly created string is directly quoted The existing string, otherwise it will be created and maintained by the dictionary.
This mechanism is called "
intern
mechanism (String intern
| string resident) ".>>> a = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_" >>> b = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_" >>> a is b True >>> id(a) 4394047056 >>> id(b) 4394047056 >>> a = "I am a string" >>> b = "I am a string" >>> a is b False >>> id(a) 4394044592 >>> id(b) 4394044656 >>> a = "^_^" >>> b = "^_^" >>> a is b False >>> id(a) 4394044720 >>> id(b) 4394044592
-
It is worth noting that:
小整数对象池
Is created by Python before running the programintern 机制(String intern | 字符串驻留)
The dictionary in is created and constantly added during the running of the program
Python 编译机制
| Code block | 小整数对象池
and字符串intern机制
For int and str, although there is a 小整数对象池
sum 字符串intern机制
to ensure that some commonly used variables are cached. But this is not all of int and str. In Python, there are other mechanisms that affect the creation of int and str .
Such Python 的编译机制
as: 代码块
, , 字符缓冲池
and so on.
I won’t go into details here, I will split an article for sorting. The link to the article will be posted here in the future.
For this part, I refer to several articles, and I feel that they are well written, you can refer to:
- Python tips: small integer object pool and string intern
- Python basics: the difference between is and ==
Reference
- The difference between == and in Python, is (this article is the most simple understanding, not correct, you can make a negative case)
- The difference between is and == in Python
- Python features: is and ==, variable memory management, small integer object pool, intern mechanism
- Python tips: small integer object pool and string intern
- Python basics: the difference between is and ==