Every ordinary reference keeps its target alive. Sometimes that is exactly wrong. A cache that holds on to every object that it has ever seen never lets any of them go. A child that points to its parent makes a cycle. A list of listeners that keeps each listener alive makes it impossible for a listener to disappear. A weak reference solves all three: it lets you reach an object while it exists, without being a reason for it to exist. This lesson shows weak references, the collections that are built from them, and finalize, the reliable way to run clean-up code.
More lessons in Complete Python Programming