(I already answered the question in to your other question , so I will also use it here, with minor changes :)
del does not delete objects; in fact, in Python, even the / VM interpreter cannot delete an object from memory, because Python is a garbage-collected language (e.g. Java, C #, Ruby, Haskell, etc.).
Instead, what del does when calling a variable (as opposed to a dictionary key or list item) is like this:
del a
lies in the fact that it only deletes a local (or global) variable, and not what it points to (each variable in Python contains a pointer / link to its contents, not the content itself). In fact, since locals and globals are stored as a dictionary under the hood (see locals() and globals() ), del a equivalent to:
del locals()['a']
(or del globals()['a'] as applied to the global.)
so if you have:
a = [] b = a
you create a list by storing a link to it in a , and then copy that link to b without copying / touching the list object itself. Therefore, these two calls affect the same object:
>>> a.append(1) >>> b.append(2) >>> a [1, 2] >>> b [1, 2] >>> a is b
( id returns the memory address of the object)
while the removal of b nothing to do with the fact that b indicates:
>>> a = [] >>> b = a >>> del b
Erik Allik Apr 30 '14 at 10:40 2014-04-30 10:40
source share