Knowing that the is
operator checks whether two variables refer to the same object, I did the following:
>>> a = 100
>>> b = 100
>>> a is b
True
>>> id(a) ; id(b)
140282048774984
140282048774984
>>> id(a) == id(b)
True
>>> b = 200
>>> id(a) ; id(b)
140282048774984
140282048778184
>>> id(a) == id(b)
False
Python's docs says that the id
function (in CPython) returns the memory address of the variable. So although I declared two variables separately, while specifying their values in a way that - at naked eye - implies no co-relation between them, they still manage to point to the same memory address.
Does python searches through memory for an equal object to which reference?
- If so, isn't that inefficient CPU-wise?
- Doesn't it elevate CPU usage when collecting garbage?