0

Al is said in the title, the following code:

class X: pass
print(id(X()) == id(X()))

prints True If I run

id(X())

multiple times, I will see different ids. Also verification with "X() is X()" returns False as expected.

Ingo
  • 67
  • 1
  • 8
  • 3
    Since you're not saving the `X` objects anywhere, the first one gets garbage collected after `id()` returns, and the second one uses the same memory. So the second one gets the same ID. – Barmar Jan 13 '21 at 22:14
  • I am getting this "This question already has an answer", but to me it's not obvious, that these question "are same". I believe your answer @Barmar is a good one, and I would like to just accept it. – Ingo Jan 13 '21 at 22:39
  • I didn't post a real answer because I consider the two questions equivalent. – Barmar Jan 13 '21 at 22:41
  • You didn't assign names to the two objects, so they're "unnamed python objects" and they got the same ID. – Barmar Jan 13 '21 at 22:42
  • I was about to close the question with the same duplicate, but Charles Duffy beat me to it. – Barmar Jan 13 '21 at 22:43

0 Answers0