2

I am storing a value in a variable and same value in another variable then it is giving the same id for both variables. but if i am assining a list to a variable and same list to another variable that ID is differing why?

my example program is:

>>> x=10
>>> y=10
>>> id(x)
21122368
>>> id(y)
21122368


>>> x=[1,2,3]
>>> y=[1,2,3]
>>> id(x)
35525896
>>> id(y)
35527736
  • 1
    Because it is a different list, where you've put the same content. – gboffi Apr 21 '16 at 07:24
  • Integers are *immutable* and hence it doesn't make a difference whether one is reused or not. Lists however are mutable and must be separate instances to provide the expected behaviour. – deceze Apr 21 '16 at 07:27
  • It;s a possible duplicate of tens (literally) of similar questions. – gboffi Apr 21 '16 at 07:29
  • I forgot to mention, while a Python programmer may need to know the memory location of an **object** (note that I haven't written _variable_), hence the inclusion of `id()` in the language, they usually doesn't. – gboffi Apr 21 '16 at 07:41
  • your saying immutable and nutable behaves different if that is the case string is also immutable but for string i am getting the same memory location allocated –  Apr 21 '16 at 10:22
  • example:>>> x="s" >>> y="s" >>> id(x) 21122368 >>> id(y) 21122368 –  Apr 21 '16 at 10:22

2 Answers2

2

This happens due to an optimization in the most popular Python implementation. Basically, small integers are not created as full objects, rather you receive a reference to an existing small integer, and it's the same one every time.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
2

Some immutable objects are cached in python, being some numbers and strings an example. That means, when you write 1 you get a reference to an object that already existed on the heap.

Lists are not cached (makes little sense, starting from the fact that they are mutable), and therefore they have different identity, although they can be equal in contents.

Maybe a little comparison helps. Each person that takes a look at the moon, is seeing the very same moon (that would be getting a reference to 1)... therefore you can say that

id(my_moon) == id(your_moon)

or just

my_moon is your_moon

Since the moon is the moon, and 1 is 1, some implementations of python decide to cache this object for performance reasons.

On the other hand, mutable objects are a horrible candidate for caching. For instance, each person that takes a bag of chips, is taking a different instance of a bag of chips, therefore it can be that at some point

my_chips == your_chips

because they have the same amount of chips, but in no case whatsoever it would be

id(my_chips) == id(your_chips)

because that would mean that the object is shared, and when somebody else eats a chip (mutates the chips bag object), a chip would dissapear from your bag... and that would be a terrible world to live in :)

bgusach
  • 14,527
  • 14
  • 51
  • 68
  • your saying immutable and nutable behaves different if that is the case string is also immutable but for string i am getting the same memory location allocated –  Apr 21 '16 at 10:20
  • example:>>> x="s" >>> y="s" >>> id(x) 21122368 >>> id(y) 21122368 –  Apr 21 '16 at 10:20
  • I don't understand your comment. Some immutable objects are cached by the Python VM for performance reasons (this is implementation dependent). Strings and Integers happen to be cached sometimes, and that is your case. That is why `1 is 1` and `'s' is 's'`. – bgusach Apr 21 '16 at 10:25