Everything is an object in Python.
The three basic elements that objects in Python contain are:
- id
- type (data type)
- value
Comparing equality between objects can be done either with == or with is.
is and == are compared to the object of the role of judgment, but the content of the object comparison judgment is not the same. Let's see what the difference is.
is compares whether the id values of two objects are equal, that is, whether the two objects are the same instance object and point to the same memory address.
== compares the contents of two objects to see if they are equal, and by default calls the object's __eq__ method.
== is the comparison operator in python's standard operators, used to compare two objects to determine if their values are equal.
Let's start with an example
>>> a = [1, 2, 3] >>> b = a >>> b is a True >>> b == a True >>> b = a[:] >>> b is a False >>> b == a True
is is also called the identity operator, which is whether the ids are the same.
Look at the following code, the ids of the a and b variables are different, so b == a is True and b is a is False.
Take a look at their ids again:
>>> id(a) 4364243328 >>> >>> id(b) 4364202696
In which cases are is and == identical?
>>> a = 256 >>> b = 256 >>> a is b True >>> a == b True >>> a = 1000 >>> b = 10**3 >>> a == b True >>> a is b False >>>
So the number types are not exactly the same.
So why is it the same at 256, but different at 1000?
Because of performance considerations, Python has done a lot of optimization work internally. For integer objects, Python caches some frequently used integer objects and saves them in a chained table called small_ints. Throughout the life cycle of Python, any place that needs to refer to these integer objects will no longer recreate new objects but will instead refer directly to the Instead of creating new objects, you can refer directly to the cached objects throughout the life of Python.
Python puts small objects in small_ints in the range [-5, 256] for those integers that may be used frequently, but whenever you need to use a small integer, you take it from there and don't create a new object on the fly.
>>> c = '' >>> d = '' >>> c is d False >>> c == d True >>> c = 'miracleyoung' >>> d = 'miracleyoung' >>> c is c True >>> c == d True
So the string types are not exactly the same, this has to do with the interpreter implementation.
>>> a = (1,2,3) # a and b are tuple types >>> b = (1,2,3) >>> a is b False >>> a = [1,2,3] # a and b are of type list >>> b = [1,2,3] >>> a is b False >>> a = {'miracle':100,'young':1} # a and b are of type dict >>> b = {'miracle':100,'young':1} >>> a is b False >>> a = set([1,2,3]) # a and b are of type set >>> b = set([1,2,3]) >>> a is b False
So when the variable is a number, a string, a tuple, a list, a dictionary, is and == are not the same and cannot be used interchangeably! When comparing values, use ==, and when comparing whether they are the same memory address, use is.
Of course, there are more cases of comparative values in development.
The above is a small introduction to the difference between is and == in Python detailed integration, I hope to help you, if you have any questions please leave me a message, I will promptly reply to you. Here also thank you very much for your support of my website!