-4

Take a look at this:

screenshot showing usage of id() function

According to python documentation, every object is given a different reference id every session... but when I run this code, as you can see id(1001) is equal to id(1002) until I run the expression id(1001) == id(1002) which is when id of 1001 changes... can anyone tell me how the heck does this work? I did check some other questions that seemed similar but couldn't find an answer.

martineau
  • 119,623
  • 25
  • 170
  • 301
Hiten Tandon
  • 64
  • 1
  • 5
  • 2
    From the [docs](https://docs.python.org/3/library/functions.html#id) - `Two objects with non-overlapping lifetimes may have the same id() value.`. Your objects only exist for the single line that you use them and have "non-overlapping lifetimes" – Iain Shelvington Apr 05 '22 at 16:34
  • 3
    First, you take the id of the anonymous object literal `1001`, which happens to be an integer. Since it is anonymous (no references / variables bound to it / pointing at it), its lifetime is limited to the scope of the expression in which you've written it, which means this object literal gets killed as soon as the expression is evaluated, and any other following object is now free to take on that same ID. Basically, by the time you do `id(1002)`, the object `1001` does not exist anymore. When you evaluate `id(1001) == id(1002)` you are forcing their lifetimes to overlap. – Paul M. Apr 05 '22 at 16:35
  • https://www.quora.com/In-Python-why-do-two-integers-with-the-same-values-have-the-same-ID-Are-they-referring-to-the-same-object https://stackoverflow.com/questions/41885782/why-are-larger-int-numbers-returning-different-and-sometimes-the-same-ids These might give you some idea – apoorva kamath Apr 05 '22 at 16:38
  • 1
    *"Is one of the most core Python functions which has been around for decades and is widely used buggy in a way that can be demonstrated with two lines of code?"* — **No.** – deceze Apr 05 '22 at 17:34
  • @deceze Actually *yes*, and I don't even need two lines: `max({})`. That claims `ValueError: max() arg is an empty sequence`, even though `{}` is *not* a sequence. – Kelly Bundy Apr 06 '22 at 16:57
  • 1
    @Kelly *smirk* Fine, but hardly a bug. The behavior is correct in every way, only the choice of the word “sequence” in the error message is arguably not entirely fitting the definition of a “sequence” as defined elsewhere. – deceze Apr 06 '22 at 17:55

1 Answers1

1

This is because of pythons reference counting!

Whenever you use any integer, string or whatever, Python creates a new object in memory for you. While it's reference count is not zero the id will be unique for that object.

When you type:

>>> id(1000)
140497411829680

Python creates 1000, returns its id and then removes the object from memory, since it is not referenced anywhere else. This is because you could otherwise fill your whole memory with only id(1000), while it is actually the same object.

this is also why:

>>> id(1000)
140697307073456
>>> id(1001)
140697307073456

returns the same id. You can try to set a variable with your integer and see the difference:

>>> a = 1000
>>> id(a)
140697307073456
>>> b = 1001
>>> id(b)
140697306008468

Here, python keeps the object and id in memory, since it's reference count is not zero!

Damiaan
  • 777
  • 4
  • 11