The identity operator is used for checking whether the object is of the mentioned type.
Example:
>> a = "Hello World!"
>> type(a) is str
>> True
The result returned in the above case is true because the type of object is str.
Identity operator is also used to compare the object with None.
Example:
>> a = None
>> a is None
>> print(a)
>> True
Internally, the identity operator compares the memory address of the objects. Built-in id()
method returns a unique number i.e address of the object in memory. If the address of both the objects in memory is the same identity operator returns the result as True.
Example:
>> a = 10
>> b = 10
>> a is b
>> True
>> id(a)
>> 916744
>> id(b)
>> 916744
The above code snippet tells us that value of both the objects is the same and variables a
and b
are referring to the same object in memory.