Python – Binding different names to equal objects, turns out Python considers them the same

Question:

I fear my question is extremely basic but in spite of an extensive search I could not make any sense of it.
I found the following snippet at Geeks for Geeks

a = "first"
b = "first"
print(id(a))
print(id(b)) 
print(a is b)  

The output being True, the snippet demonstrates how names are bound to immutable objects such as a string.

I am confused as to how does the process work.
During the first assignment

a = "first"

an immutable object is created (the string) and it is then bound to the variable name, a.

During the second assignment, somehow it is understood that the object on the right hand side exists already, and the new variable name, b, is bound to the same object as before.

How is this achieved? So every time an assignment statement is issued, Python "checks" is an equal object exists already?

And what if I wanted to create a new, separate object, that happens to be equal to a previously defined one, and yet I want the two not to be the same object?

So two different variable names point to two different objects (say two strings), and the two strings just happen to be equal?

I wonder this possibility is useless for immutable objects, but I am not so sure.

Thanks a lot

Asked By: user37292

||

Answers:

As far as I understand, there is no guarantee that a is b for your example code. It probably is a CPython optimization.

The documentation of id() says:

This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.

CPython implementation detail: This is the address of the object in memory.

So the question is if there will be two string objects created by your code, or just one.
Apparently, the interpreter recognizes that the two strings are similar and reuses the same object, which is possible since strings are immutable.

And what if I wanted to create a new, separate object, that happens to be equal to a previously defined one, and yet I want the two not to be the same object?

I wonder why you would even care about that (besides curiosity / learning purposes). From a practical point of view it does not really matter, as long as the code works correctly.
But I can demonstrate that an object will not automatically get the same id just because it has the same value:

>>> a = 'first'
>>> b = 'fir' + 'st'
>>> a is b
True
>>> c = 'fir'
>>> c += 'st'
>>> a is c
False
>>> a == b == c
True

Also, for mutable objects, the ids will always be different:

>>> x = []
>>> y = []
>>> x is y
False
Answered By: wovano