To add a few points to above answers:
del x
Definition of x
indicates r -> o
(a reference r
pointing to an object o
) but del x
changes r
rather than o
. It is an operation on the reference (pointer) to object rather than the object associated with x
. Distinguishing between r
and o
is key here.
locals()
.globals()
if x
belongs there.x
belongs to, not where x
points to. The only physical change in memory is this. For example if x
is in a dictionary or list, it (as a reference) is removed from there(and not necessarily from the object pool). In this example, the dictionary it belongs is the stack frame (locals()
), which overlaps with globals()
.