Everything is an object in Python.

Objects in Python contain three basic elements, which are:

  • Id (Identity identification)
  • Type (data type)
  • The value (value)

Objects can be compared for equality using == or is.

Both is and == are used to compare and judge objects, but the contents of comparison and judgment are different. Now what’s the difference?

Is compares whether the IDS of two objects are the same. That is, it compares whether two objects are the same instance object and refer to the same memory address.

== compares whether the contents of two objects are equal, and the __eq__ method of the objects is called by default.

== is the comparison operator of Python’s standard operators, used to determine whether two objects have equal values.

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
Copy the code

Is is also called the identity operator, which is whether the IDS are the same or not.

A and B have different ids, so b == a is True, b is a is False

Let’s look at their ids again:

>>> id(a)
4364243328
>>> 
>>> id(b)
4364202696
Copy the code

When 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
>>>
Copy the code

So the number types are not exactly the same.

So why is it the same at 256 and different at 1000?

For integer objects, Python caches some of the most frequently used integer objects and stores them in a linked list called Small_ints. Throughout Python’s life, wherever we need to refer to these integer objects, Instead of recreating new objects, they refer directly to objects in the cache.

Python places these potentially frequently used integer objects in the range [-5, 256] in Small_ints, but whenever small integers are needed, they are taken from there instead of creating new objects temporarily.

>>> c = 'miracle.young'
>>> d = 'miracle.young'
>>> c is d
False
>>> c == d
True
>>> c = 'miracleyoung'
>>> d = 'miracleyoung'
>>> c is c
True
>>> c == d
True
Copy the code

So the string types are not exactly the same, and it depends on the interpreter implementation.

>>> a = (1.2.3) # a and b are tuples
>>> b = (1.2.3)
>>> a is b
False
>>> a = [1.2.3] A and b are types of list
>>> b = [1.2.3]
>>> a is b
False
>>> a = {'miracle':100.'young':1} A and b are dict types
>>> b = {'miracle':100.'young':1}
>>> a is b
False
>>> a = set([1.2.3]) # a and b are set types
>>> b = set([1.2.3])
>>> a is b
False
Copy the code

So when variables are numbers, strings, tuples, lists, dictionaries, is and == are not the same and cannot be used interchangeably! Use == when comparing values, and use is when comparing whether it is the same memory address. Of course, there is a lot of comparison in development.

Pay attention to the public number “Python column”, the background reply “Tencent architecture resources 1”, get the full set of source code for this article!