-1

I have 2 variables (links) a and b that are pointing to the same object, and, of course

a is b #true

But when im asking about memory addr for the same object that this links pointing to

id(a) is id(b) #false

Does that mean that id is creating a new object ? How long will this object last ? What type will it be? What's going on?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
nachtblume
  • 57
  • 5
  • 3
    id returns an integer. Use `==` to compare integers, not `is`. – khelwood Apr 10 '22 at 19:03
  • You can also refer to the Python documentation: https://docs.python.org/3/library/functions.html#id – VoidTwo Apr 10 '22 at 19:04
  • So id returning int, and this int is stored as int object...but what happens to the link of this new object ? – nachtblume Apr 10 '22 at 19:07
  • What im trying to say is - basically id(a) has to store this data somewhere in memory right ? so py must create a new object with int type(ty), link, gc data and this int. What will happen with the link? – nachtblume Apr 10 '22 at 19:10
  • The `int` object returned by `id()` will last exactly as long as anything has a reference to it - *exactly like every other object in Python*. – jasonharper Apr 10 '22 at 19:10
  • @jasonharper id(a) is id(b) - here..i don't really understand the reference part. When id(a) is called will it create a reference by itself ? – nachtblume Apr 10 '22 at 19:13
  • 2
    `id()` will return a reference - because no other possibility exists for Python functions. Whether that reference is to an existing `int` object, or a brand-new `int` object, is not something you should ever need to care about - but in practice, it will almost certainly be a new object in this particular situation. Therefore the `is` test produces False, even though the two int objects have the same numeric value. – jasonharper Apr 10 '22 at 19:20
  • @jasonharper, ok, i get it, but when for example we are doing something like a = id(b) this new int object will have a as it's reference, but if we are just calling id(a) what reference will this object get. When we are calling a function without any variable assignment will this new created object has some sort of None or False as data in refference field and dissapear immediately ? – nachtblume Apr 10 '22 at 19:28
  • 1
    Variables aren't the only form of reference - a function call in progress temporarily holds references to all of the parameters (so they cannot disappear in the middle of the call), a list holds references to all of its elements, etc. Note that an object has no knowledge about *what* is referencing it, it only has a count of references (which triggers deletion when it goes to zero). – jasonharper Apr 10 '22 at 19:36
  • @jasonharper, so this id(a) object is created with ref count as 1, after the call it becomes 0 and object goes to a better place ? If function is being called the ref part in object that was created equal to 1 ? – nachtblume Apr 10 '22 at 19:39

2 Answers2

1
>>> a is b
True 

They are the same object...

>>> id(a) == id(b)
True 

They have the same id...

>>> id(a) is id(b)
False 

...but the integers representing their ids are not the same object.

That's because in Python ints are objects too.


I would suggest you to read this, this and this.

FLAK-ZOSO
  • 3,873
  • 4
  • 8
  • 28
  • Yeah, this is basically an answer. Ty FLAK-ZOSO and @jasonharper, now i just have to wrap my head around reference creation part.. – nachtblume Apr 10 '22 at 19:17
  • You'll probably want to read https://nedbatchelder.com/text/names.html as well. – chepner Apr 10 '22 at 19:26
0

Okay, the following answer will get very wordy but hopefully lucid enough... If any clarification is required please let me know.

In the first statement a is b, you are basically comparing the address of the two objects (or pointers/references to the same object). So you are asking if a is the same as b, and you get True.

But in the second statement id(a) is id(b) you aren't comparing the addresses of a and b. You are comparing the address of the two objects that are holding the value of the addresses of a and b.

Still confused? Let's see an example..

a = [1]
b = a
a is b      #True

The reason for the above output: a and b are pointing to the same list object

address1 = id(a)
address2 = id(b)
address1 is address2      #False

The reason for the above output: address1 and address2 are integers and while they contain the same value, those values are stored in different objects. Similarly when you run id(a) is id(b), you are comparing the two different address-value-holding objects and not the object addresses.

SOLUTION:

Just type,

id(a) == id(b) # This will tell you whether the location of a and b is the same. 
#If the output is true, then a and b point to the same object.

Hope that makes things clearer!

Eshita Shukla
  • 791
  • 1
  • 8
  • 30