Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internals for python tuples

>>> a=1
>>> b=1
>>> id(a)
140472563599848
>>> id(b)
140472563599848
>>> x=()
>>> y=()
>>> id(x)
4298207312
>>> id(y)
4298207312
>>> x1=(1)
>>> x2=(1)
>>> id(x1)
140472563599848
>>> id(x2)
140472563599848

until this point I was thinking there will be only one copy of immutable object and that will be shared(pointed) by all the variables.

But when I tried, the below steps I understood that I was wrong.

>>> x1=(1,5)
>>> y1=(1,5)
>>> id(x1)
4299267248
>>> id(y1)
4299267320

can anyone please explain me the internals?

like image 885
Yogeswaran Avatar asked Dec 13 '14 14:12

Yogeswaran


People also ask

How tuple works internally in Python?

Python has two similar sequence types such as tuples and lists. The most well-known difference between them is that tuples are immutable, that is, you cannot change their size as well as their immutable objects. Internally, both lists and tuples are implemented as a list of pointers to the Python objects (items).

What is inside a tuple?

Tuples are used for grouping data. Each element or value that is inside of a tuple is called an item. Tuples have values between parentheses ( ) separated by commas , .

What are the Python properties of a tuple?

A tuple is a collection of objects which ordered and immutable. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.


1 Answers

>>> 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 ids 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 ids.

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.

like image 180
thefourtheye Avatar answered Oct 04 '22 17:10

thefourtheye