1
for a in range(2):
    print(id({}))  # 2444803983552, 2444803983552

for a in range(2):
    obj = {}
    print(id(obj))  # 2347467557056, 2347469009408

Why does it work so? I thought that in both ways dict would relate to different memory units

ggindinson
  • 21
  • 7
  • 1
    I think it's because the first time you're looking up for a literal value, whereas the second time, you're assigning that literal value to an object, then looking up on that object. – Zero Jul 19 '23 at 11:49
  • You can see the same thing with `x = {} ; print(id({}), id({}), id(x))` – mozway Jul 19 '23 at 12:04
  • 1
    When objects get garbage collected or are no longer referenced by anything, the id may be re-used for new objects. See the same example with a custom class https://pastebin.com/kvAHmXEd – flakes Jul 19 '23 at 12:25

0 Answers0