Why is python storing these two variables at the same location?

Viewed 781

Consider the following code:

x = 50000000
y = 50000000

print(hex(id(x)), hex(id(y)))
print(x is y)

This gives:

0x23ef28ec930 0x23ef28ec930
True

Now I was lead to believe that for small values python uses optimisation to cache these in memory. But it the number is quite clearly larger than this value which I believe is 256.

Why is it not when I declare these they are stored separately in memory?

Further to this if I add in the line y+=1 just before the print statement then x and y are stored in separate locations in memory and x is y is False. I would have thought that as the are both the same object in memory that incrementing y would increment x also.

Clearly I am missing something fundamental and would appreciate it being cleared up.

As a further point does this behaviour depend on the variable type? What about if x,y are not ints but lists, strings or any other object?

Thanks!

2 Answers

python use referencing for more efficient memory usage. So, instead of two variables that share the same value and occupying two separate locations in memory, python references one of them to the other.

https://docs.python.org/2.0/ext/refcounts.html

x = 50000000

Here, Python creates an int and stores it at 0x23ef28ec930.

y = 50000000

Here, the interpreter was able to somehow detect that the number already exists in the program, and so used the same object for y that it uses for x. Since these are number literals, the number 50000000 may have been stored at compile time and then used for both of these assignments.

As far as I know, Python makes no guarantees about whether or not identical immutable values will have the same address. You should not rely on this behavior except when dealing with singletons, like None, True, and False.

Further to this if I add in the line y+=1 just before the print statement then x and y are stored in separate locations in memory and x is y is False. I would have thought that as the are both the same object in memory that incrementing y would increment x also.

ints are immutable. When you do y += 1, you are assigning a new value to y, which makes it different from x.

As a further point does this behaviour depend on the variable type? What about if x,y are not ints but lists, strings or any other object?

It depends a lot on whether or not the values are mutable or immutable. Strings are immutable, so identical ones might have the same address. Lists are mutable, so it makes a big difference whether x and y refer to the same list or to two newly constructed lists. These two snippets are very different:

# These have different addresses. They can be changed independently.
x = []
y = []

# These variables refer to the same list.
x = []
y = x

Note that operators like += attempt to change the object in place. This is obviously not possible with immutable values, but for lists, y += [6] modifies the existing list, while y = y + [6] makes a new list. += is not always equivalent to plain addition.

Related