3

Consider the following log:

>>> y = 20000
>>> id(y)
36638928
>>> y = 1000000
>>> id(y)
36639264

As you can see, after changing the value of y, it's id changed as well.
Does it mean that int is immutable? what is happening behind the scenes?

Thanks!

SuperStamp
  • 155
  • 1
  • 5
  • 1
    yes it's immutable, it just affected a new variable to the name `y`. Feel free to google these things online, there are plenty of resources online already.. – Loïc Faure-Lacroix Jan 30 '14 at 22:29
  • 4
    You did not change the value stored in `y`, but assigned a new value to `y`. This would change the `id` even if that value was mutable. – tobias_k Jan 30 '14 at 22:29
  • 2
    I recommend reading http://bit.ly/pynames (to every python programmer ever, really). – Wooble Jan 30 '14 at 22:42
  • 3
    This has nothing to do with the fact that `int`'s are immutable. On the right hand side of the assignments are *different* objects, that's what matters. – Reinstate Monica Jan 30 '14 at 23:24

1 Answers1

6

Yes, integers are immutable. What you need to realize is that:

  1. A variable is simply a name which you use to reference an object.

  2. 20000 and 1000000 are two unique integer objects. This means that they will never share the same memory address simultaneously.

In simple terms, when you execute this line:

y = 20000

two things happen:

  1. An integer object 20000 is created in the object space.

  2. A name y is created in the namespace and pointed to that object.

When you execute this one:

y = 1000000

two more things happen:

  1. A new integer object 1000000 is created in the object space.

  2. The name y is changed to point to that object instead of 20000.

  • I saw what you did there! – Loïc Faure-Lacroix Jan 30 '14 at 22:30
  • this makes it sounds like `x=10`, `y=10` then `id(x) != id(y)` which is not true, there is only one occurrence of each integer and both x and y reference it. – cmd Jan 30 '14 at 22:49
  • 4
    @cmd: that's sort of an implementation detail, `int`'s are occasionally interned, but the circumstances under which they are interned is not a defined part of the language; for example; on my machine (cpython 2.7.6) `1000 is 1000` but `1000 is not (999 + 1)` – SingleNegationElimination Jan 30 '14 at 23:22
  • To be pedantic, 20000 and 100000 could end up in the same place in memory at different times, if whichever one was there first was no longer referenced and got deallocated. – Wooble Jan 30 '14 at 23:31
  • It is not "given the integer object as a value" - the correct phrase is _"A name `y` is created in the namespace and is pointed to the object `20000` that is created in the object space"_ as this is actually what is happening, and a unique distinction in Python. Variables are not "boxes to hold things" (that's why they are called _names_ and not _variables_ in Python). – Burhan Khalid Jan 31 '14 at 00:21
  • Well, I'll add a little more detail. However, I was trying to keep it very simple for the OP. I think a super detailed answer will confuse him even more. –  Jan 31 '14 at 01:28