Object destruction: Garbage collection* using reference counting

The other thing to note is that the assignment lst[1] = lst[0] not only causes lst[1] to refer to 'Tom' like lst[0] does it also triggers the destruction of the object 'Tim'. It does so because Python uses reference counting to decide when an object can safely be destroyed and the memory it is using reclaimed. Reference counting garbage collection is based on the insight that once the number of references to an object declines to 0 that object can never be accessed by a program, therefore it is safe to destroy it. This happens to 'Tim' when the last reference to it (lst[1]) is removed.

This is also what happens to local variables in functions when the function ends: as the local names are deleted the reference counts to the objects drop to 0 and they can be destroyed (unless they are passed back by a return statement which creates a reference to them from a name in the calling function).

In terms of our diagrams an object's reference count is the number of arrow heads pointing to it. At the end of the previous code fragment the reference count of 'Matt' is 1, and the reference count of 'Tom' is 3.

With practice these semantics will become second nature, but at first you may find it necessary to draw diagrams like those above when debugging your code.