2

Initially variables a, b and c all have value 1 and same address. When variable a is incremented by 1 then address gets altered, while the address of variables b and c remains same. Can someone elaborate on this address allotment?

Also now when variable b is incremented by 1 and address of b now equals to address of a. Can someone please elaborate on this as well?

>>> a = 1
>>> b = a
>>> c = b
>>> a += 1
>>> print a,b,c
2 1 1
>>> id(a)
26976576
>>> id(b)
26976600
>>> id(c)
26976600
>>> b += 1
>>> print a,b,c
2 2 1
>>> id(c)
26976600
>>> id(b)
26976576
>>> id(a)
26976576
1
  • Please read nedbatchelder.com/text/names.html. Also note that the second part of your question is an implementation detail - CPython interns small integers, this isn't behaviour you should rely on. Commented Jul 12, 2015 at 14:44

3 Answers 3

1

Values and memory addresses are all misleading terms. Think of objects, names and IDs. First the object 1 is assigned to the names a, b and c. So the ID of this object can be reached by all the names.

In the second step, you assign a new object, the integer 2, with other ID to the name a.

In the third step, you assign the object integer 2 to b also. This is a implementation detail of CPython, that small integers are only held once in memory, so the object, and therefore its ID, that is reached by the name b is the same as by a.

Sign up to request clarification or add additional context in comments.

Comments

1

https://docs.python.org/2/c-api/int.html#c.PyInt_FromLong

The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object.

Also, In Python, Integer comes from a immutable object: PyIntObject. Once you create a PyIntObject, you'll never change it's value, and the others is just reference.

Comments

0

The reason you are seeing this is because you are thinking in terms of the variables, a, b, and c being the objects, when in fact it is the integers that are the objects of your example. The memory location that you are looking at when you type id() is the location for the int object 1 and the int object 2, and not the variable names a, b, and c.

In python, names are bound to objects. This can be problematic when you have types like lists which are mutable and you try to copy them. Changing the object means that when you poll both references, you see the change propagated in both, and that isn't always what you intend, and unless you are aware of it can cause a lot of problems with debugging.

Back to your example, I added two instances of id() at the beginning to show the id's of the integer objects 1 and 2. This should clarify things for you.

>>> a = 1
>>> b = a
>>> c = a
>>> id(a)
4298174296
>>> id(b)
4298174296
>>> id(c)
4298174296
>>> id(1)
4298174296
>>> id(2)
4298174272
>>> a += 1
>>> id(a)
4298174272
>>> id(b)
4298174296
>>> id(c)
4298174296
>>> b += 1
>>> print a, b, c
2 2 1
>>> id(c)
4298174296
>>> id(b)
4298174272
>>> id(a)
>>> 4298174272

As you can see, the location for 1 and a b c are initially all the same and the location for 2 is different. Then when you change the assignment for a, it points to the location of 2, while b and c stay pointed to 1. Then when you reassign b, it points to the location for 2 as well, leaving only c pointing at 1.

Hope that clarifies it for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.