- VC Healy
- May 23, 2020
- 1 min read
Updated: May 24, 2020
The comparison operators.
Comparison of the object values
Comparing the value of held at the memory address for each variable
== are the values equivalent
a==a , True
is comparison of the object identity.
Using the same memory address. A simple example of this is to use a singleton. These are the values assigned a memory by default like the low value integers.
We know 3 == 3 is True, as they are both of the same numerical value but when you realise that the number is no more a label than the 'letter a' you begin to understand the logic behind the is operator.
3 is 3. What this statement is asking ...
Does the label 3 on the left point to the same memory address as the label 3 on the right?
It doesn't matter what the value content of the memory address is, if both of the labels point to the same address then the 3 is 3 would return the answer True.
You have to watch using the is operator,
a = 10_000
b = 10_000
Asking this will give different answers:
a==b : True # Because the value is the same for each variable
a is b: False # The memory location, in this case, is different for each variable, even if the two locations happen to contain the same content.