14

id(object)

This is an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime.

Can you explain this output? Why does j's id change?

>>> i=10  
>>> id(i)  
6337824  
>>> j=10  
>>> id(j)  
6337824  
>>> j=j+1  
>>> id(j)  
6337800  
>>> id(i)  
6337824  
Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196
anon
  • 1,071
  • 2
  • 8
  • 19
  • is it like i and j are object references to the same object initially.. but when j changes, they start referring to different objects? – anon Aug 04 '10 at 05:00
  • 4
    For why they're initially the same, see [ Python “is” operator behaves unexpectedly with integers ](http://stackoverflow.com/questions/306313/python-is-operator-behaves-unexpectedly-with-integers). Bascially, ints are immutable objects, but small ones are all kept interned as an optimization. – Matthew Flaschen Aug 04 '10 at 05:02

8 Answers8

35

Because integers are immutable, each integer value is a distinct object with a unique id. The integer 10 has a different id from 11. Doing j=j+1 doesn't change the value of an existing integer object, rather it changes j to point to the object for 11.

Check out what happens when we independently create a new variable k and assign it the value 11:

>>> j=10
>>> id(j)
8402204
>>> j=j+1
>>> id(j)
8402192
>>> k=11
>>> id(k)
8402192

Note that it is not always the case that every integer has one and only one corresponding object. This only happens for small integers that Python decides to cache. It does not happen for large integers:

>>> x = 123456789
>>> id(x)
8404568
>>> y = 123456789
>>> id(y)
8404604

See https://docs.python.org/3/c-api/long.html#c.PyLong_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.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
16

This is why 2**8 is 2**8 == True, and 2**9 is 2**9 == False.

Values between -5 and 256 are preallocated.

pjvandehaar
  • 1,070
  • 1
  • 10
  • 24
Ning Sun
  • 2,145
  • 1
  • 19
  • 24
2

The same id for different variables is a product of how Python creates variables.

id is a hash of the the location of an object in memory. Python variables are references to an object, not new objects. If several variables reference the same object, they have the same `id.

Tim McNamara
  • 18,019
  • 4
  • 52
  • 83
1

In CPython, id is generally derived from the Py_Object's pointer value, that is its location in memory.

Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
1

Python caches immutable objects(read integers and tuples..) - which is why they are immutable and saves memory if u reference the same immutable in many places. So small integers, empty tuples and such are actually cached in Python runtime, so you keep getting back the same object and hence the same id.

Try this for the list, you don't get the same id.

jmd_dk
  • 12,125
  • 9
  • 63
  • 94
  • Python doesn't cache all immutable objects. In fact, it doesn't even cache all integers. See [this answer](http://stackoverflow.com/questions/306313/python-is-operator-behaves-unexpectedly-with-integers/306353#306353). – Matthew Flaschen Aug 04 '10 at 05:04
0

These are primitive types, so I'm guessing each value gets its own ID. Try creating a true object and I think you'll see the functionality you expect.

If you need an id for a primitive, you could create an object with just one member of that type.

Anthony DiSanti
  • 516
  • 8
  • 9
0

j's id changes because the object named by j changes. First you initialize j to 10, so when you call id(j) you get the id of 10. Then you set j to 11, so after that when you call id(j) you get the id of 11.

David Z
  • 128,184
  • 27
  • 255
  • 279
0

The thing is that, each object(value) has a unique id in Python, e.g objects 'hello' and 'hi' have their own ids or 10 and 11 have 2 other different ids. The other thing is that each variable is just a label for a specific objecect(value. Now, when we say j=10, the id of the object 10 is assigned to the variable j. Then when you change the value of j (e.g j=j+1), j will refer to another object. In other words j will become label of another object. So the id for j varies when it's value changes. There is also Python's caching approach which help it to be more efficient. For example when you write j=10 and x=10 rather creating 2 separate objects it creates just one object 10 with 2 labels j and x.

A.Ajorian
  • 139
  • 1
  • 8