Akanksha Singh5,082 Points
How do del and pop keyword differ in terms of garbage collection?
I know del removes the object completely and gives it for garbage collecion, and pop can be used to retrieve value from the given or last index into a variable, but when we use pop without any variable assignment to it, does it also go for garbage collection?
Chris FreemanTreehouse Moderator 58,944 Points
Good question. More precisely,
del removes the label that points to an object. If the deleted label is the last reference to an object, then it is available for garbage collection. Same for
pop, if it causes the last reference to an object to be removed then the object is also available for garbage collection.
Consider the following:
a = [1, 2, 3] l = [a, 'b', 'c'] del l print(1, a) a = [1, 2, 3] l = [a, 'b', 'c'] del a print(2, l)
Even though the label
a was deleted, the object that was pointed to by
a is still pointed to by