Some friends and I were discussing things related to memory management in Python when we stumbled upon the behaviour below:
In [46]: l = ({} for _ in range(6))
In [47]: [ id(i) for i in l]
Out[47]:
[4371243648, # A
4371245048, # B
4371243648, # A
4371245048, # B
4371243648, # etc.
4371245048]
What is surprising here is that we don't seem to have well defined behaviours: the dict is neither a new one each time nor the same reference each time.
On top of that, we got this weird behaviour (not code was run in the interpreter between these two snippets).
In [48]: m = ({} for _ in range(6))
In [49]: [ id(i) for i in m]
Out[49]:
[4371154376, # C
4371245048, # B (same B as above!)
4371154376, # C
4371245048, # B
4371154376,
4371245048]
Can anyone explain this behaviour? Using list comprehensions (l = [{} for _ in range(6)]
) shows different addresses for each dict.