0

Why do I only get 2 different IDs here? What happened underneath?

I tried to disable gc here, it doesn't affect the results.

And only once in a while I get 3 different IDs, but mostly I got only 2.

class A: 
    pass

for i in range(10):
    a = A()
    print(id(a))

# output
4565372768
4565372880
4565372768
4565372880
4565372768
4565372880
4565372768
4565372880
4565372768
4565372880
jonrsharpe
  • 107,083
  • 22
  • 201
  • 376
ruxtain
  • 126
  • 1
  • 11

1 Answers1

0

When it ran loop for i=0, then it allocated memory (memory adress:4565372768) for that instance of A. when it ran for second time i.e., i=1 as memory at that adress is still occupied by that instance it has allocated some other location (memory adress:4565372880) to store instance of the class. when it ran for third time i.e., i=2 the previous memory was freed and as memory location (adress: 4565372768) is free third time it allocated same location again.

Tilak Putta
  • 688
  • 4
  • 18