weak references python does automatic memory management (reference counting for most objects and garbage collection to eliminate cyclesthe memory is freed shortly after the last reference to it has been eliminated this approach works fine for most applications but occasionally there is need to track objects only as long as they are being used by something else unfortunatelyjust tracking them creates reference that makes them permanent the weakref module provides tools for tracking objects without creating reference when the object is no longer neededit is automatically removed from weakref table and callback is triggered for weakref objects typical applications include caching objects that are expensive to createimport weakrefgc class adef __init__(selfvalue)self value value def __repr__(self)return str(self valuea ( create reference weakref weakvaluedictionary( ['primary' does not create reference ['primary'fetch the object if it is still alive del remove the one reference gc collect(run garbage collection right away ['primary'entry was automatically removed traceback (most recent call last)file ""line in ['primary'entry was automatically removed file " :/python /lib/weakref py"line in __getitem__ self data[key](keyerror'primary tools for working with lists many data structure needs can be met with the built-in list type howeversometimes there is need for alternative implementations with different performance trade-offs the array module provides an array(object that is like list that stores only homogeneous data and stores it more compactly the following example shows an array of numbers stored as two byte unsigned binary numbers (typecode " "rather than the usual bytes per entry for regular lists of python int objectsfrom array import array array(' '[ ]sum( [ : array(' '[ ]the collections module provides deque(object that is like list with faster appends and pops from the left side but slower lookups in the middle these objects are well suited for implementing queues and breadth first tree searches weak references