>>> x1=(1)
>>> x2=(1)
is actually the same as
>>> x1=1
>>> x2=1
In Python, smaller numbers are internally cached. So they will not be created in the memory multiple times. That is why id
s of x1
and x2
are the same till this point.
An one element tuple should have a comma at the end, like this
>>> x1=(1,)
>>> x2=(1,)
When you do this, there are two new tuples to be constructed with only one element in it. Even though the elements inside the tuples are the same, they both are different tuples. That is why they both have different id
s.
Lets take your last example and disassemble the code.
compiled_code = compile("x1 = (1, 5); y1 = (1, 5)", "string", "exec")
Now,
import dis
dis.dis(compiled_code)
would produce something like this
1 0 LOAD_CONST 3 ((1, 5))
3 STORE_NAME 0 (x1)
6 LOAD_CONST 4 ((1, 5))
9 STORE_NAME 1 (y1)
12 LOAD_CONST 2 (None)
15 RETURN_VALUE
It loads a constant value, referred by the index 3
, which is (1, 5)
and then stores it in x1
. The same way, it loads another constant value, at index 4
and stores it in y1
. If we look at the list of constants in the code object,
print(compiled_code.co_consts)
will give
(1, 5, None, (1, 5), (1, 5))
The elements at positions 3
and 4
are the tuples which we created in the actual code. So, Python doesn't create only one instance for every immutable object, always. Its an implementation detail which we don't have to worry much about anyway.
Note: If you want to have only one instance of an immutable object, you can manually do it like this
x1 = (1, 5)
x2 = x1
Now, both x2
and x1
will refer the same tuple object.
tuple(range(3))
several times is not interned and you get separate objects. You only get interning when the compiler can figure out the value. – Summonsx1=(1)
does not make a tuple. – Consume