0

I have a question about the id() function:

Can I say that: when an object is created whether you use the id function or not it will in the background "create" an id number, this reference's the location of this object. If you don't call the object to a name this id will get cleared away because of the garbage collector?

vaultah
  • 44,105
  • 12
  • 114
  • 143
  • 3
    The [docs](https://docs.python.org/3/library/functions.html#id) are pretty clear about this: "Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value." Note, the fact that this number *happens* to be a memory address is an implementation detail of CPython that should not be relied upon. – juanpa.arrivillaga Aug 23 '17 at 19:51
  • https://stackoverflow.com/questions/15667189/what-does-id-function-used-for may be relevant. – Peter DeGlopper Aug 23 '17 at 19:52

1 Answers1

0

id() is guaranteed to be unique and constant for the object during its lifetime, and will not be affected by "garbage collection".

Check out the answer to this for more insight: What does id( ) function used for?

ajb
  • 1
  • 1
  • id is unique and constant during its lifetime but whats meant by that? will it be endend if its not attached to an string? which would result in the gc eventually? – Stefano Caruana Aug 24 '17 at 09:58
  • "unique and constant during its lifetime" means a reference that will not change whilst that object exists. Because Python's implementation of id() is based on the variable's address in memory, once a variable is garbage collected, a new variable could assume the id() of the previous object. See https://stackoverflow.com/questions/20753364/why-is-the-id-of-a-python-class-not-unique-when-called-quickly – ajb Aug 24 '17 at 12:08