weakref — Weak references

Source code:Lib/weakref.py


Theweakref module allows the Python programmer to createweakreferences to objects.

In the following, the termreferent means the object which is referred toby a weak reference.

A weak reference to an object is not enough to keep the object alive: when theonly remaining references to a referent are weak references,garbage collection is free to destroy the referent and reuse its memoryfor something else. However, until the object is actually destroyed the weakreference may return the object even if there are no strong references to it.

A primary use for weak references is to implement caches ormappings holding large objects, where it’s desired that a large object not bekept alive solely because it appears in a cache or mapping.

For example, if you have a number of large binary image objects, you may wish toassociate a name with each. If you used a Python dictionary to map names toimages, or images to names, the image objects would remain alive just becausethey appeared as values or keys in the dictionaries. TheWeakKeyDictionary andWeakValueDictionary classes supplied bytheweakref module are an alternative, using weak references to constructmappings that don’t keep objects alive solely because they appear in the mappingobjects. If, for example, an image object is a value in aWeakValueDictionary, then when the last remaining references to thatimage object are the weak references held by weak mappings, garbage collectioncan reclaim the object, and its corresponding entries in weak mappings aresimply deleted.

WeakKeyDictionary andWeakValueDictionary use weak referencesin their implementation, setting up callback functions on the weak referencesthat notify the weak dictionaries when a key or value has been reclaimed bygarbage collection.WeakSet implements theset interface,but keeps weak references to its elements, just like aWeakKeyDictionary does.

finalize provides a straight forward way to register acleanup function to be called when an object is garbage collected.This is simpler to use than setting up a callback function on a rawweak reference, since the module automatically ensures that the finalizerremains alive until the object is collected.

Most programs should find that using one of these weak container typesorfinalize is all they need – it’s not usually necessary tocreate your own weak references directly. The low-level machinery isexposed by theweakref module for the benefit of advanced uses.

Not all objects can be weakly referenced. Objects which support weak referencesinclude class instances, functions written in Python (but not in C), instance methods,sets, frozensets, somefile objects,generators,type objects, sockets, arrays, deques, regular expression pattern objects, and codeobjects.

Changed in version 3.2:Added support for thread.lock, threading.Lock, and code objects.

Several built-in types such aslist anddict do not directlysupport weak references but can add support through subclassing:

classDict(dict):passobj=Dict(red=1,green=2,blue=3)# this object is weak referenceable

CPython implementation detail: Other built-in types such astuple andint do not support weakreferences even when subclassed.

Extension types can easily be made to support weak references; seeWeak Reference Support.

When__slots__ are defined for a given type, weak reference support isdisabled unless a'__weakref__' string is also present in the sequence ofstrings in the__slots__ declaration.See__slots__ documentation for details.

classweakref.ref(object[,callback])

Return a weak reference toobject. The original object can be retrieved bycalling the reference object if the referent is still alive; if the referent isno longer alive, calling the reference object will causeNone to bereturned. Ifcallback is provided and notNone, and the returnedweakref object is still alive, the callback will be called when the object isabout to be finalized; the weak reference object will be passed as the onlyparameter to the callback; the referent will no longer be available.

It is allowable for many weak references to be constructed for the same object.Callbacks registered for each weak reference will be called from the mostrecently registered callback to the oldest registered callback.

Exceptions raised by the callback will be noted on the standard error output,but cannot be propagated; they are handled in exactly the same way as exceptionsraised from an object’s__del__() method.

Weak references arehashable if theobject is hashable. They willmaintain their hash value even after theobject was deleted. Ifhash() is called the first time only after theobject was deleted,the call will raiseTypeError.

Weak references support tests for equality, but not ordering. If the referentsare still alive, two references have the same equality relationship as theirreferents (regardless of thecallback). If either referent has been deleted,the references are equal only if the reference objects are the same object.

This is a subclassable type rather than a factory function.

__callback__

This read-only attribute returns the callback currently associated to theweakref. If there is no callback or if the referent of the weakref isno longer alive then this attribute will have valueNone.

Changed in version 3.4:Added the__callback__ attribute.

weakref.proxy(object[,callback])

Return a proxy toobject which uses a weak reference. This supports use ofthe proxy in most contexts instead of requiring the explicit dereferencing usedwith weak reference objects. The returned object will have a type of eitherProxyType orCallableProxyType, depending on whetherobject iscallable. Proxy objects are nothashable regardless of the referent; thisavoids a number of problems related to their fundamentally mutable nature, andprevents their use as dictionary keys.callback is the same as the parameterof the same name to theref() function.

Accessing an attribute of the proxy object after the referent isgarbage collected raisesReferenceError.

Changed in version 3.8:Extended the operator support on proxy objects to include the matrixmultiplication operators@ and@=.

weakref.getweakrefcount(object)

Return the number of weak references and proxies which refer toobject.

weakref.getweakrefs(object)

Return a list of all weak reference and proxy objects which refer toobject.

classweakref.WeakKeyDictionary([dict])

Mapping class that references keys weakly. Entries in the dictionary will bediscarded when there is no longer a strong reference to the key. This can beused to associate additional data with an object owned by other parts of anapplication without adding attributes to those objects. This can be especiallyuseful with objects that override attribute accesses.

Note that when a key with equal value to an existing key (but not equal identity)is inserted into the dictionary, it replaces the value but does not replace theexisting key. Due to this, when the reference to the original key is deleted, italso deletes the entry in the dictionary:

>>>classT(str):pass...>>>k1,k2=T(),T()>>>d=weakref.WeakKeyDictionary()>>>d[k1]=1# d = {k1: 1}>>>d[k2]=2# d = {k1: 2}>>>delk1# d = {}

A workaround would be to remove the key prior to reassignment:

>>>classT(str):pass...>>>k1,k2=T(),T()>>>d=weakref.WeakKeyDictionary()>>>d[k1]=1# d = {k1: 1}>>>deld[k1]>>>d[k2]=2# d = {k2: 2}>>>delk1# d = {k2: 2}

Changed in version 3.9:Added support for| and|= operators, as specified inPEP 584.

WeakKeyDictionary objects have an additional method thatexposes the internal references directly. The references are not guaranteed tobe “live” at the time they are used, so the result of calling the referencesneeds to be checked before being used. This can be used to avoid creatingreferences that will cause the garbage collector to keep the keys around longerthan needed.

WeakKeyDictionary.keyrefs()

Return an iterable of the weak references to the keys.

classweakref.WeakValueDictionary([dict])

Mapping class that references values weakly. Entries in the dictionary will bediscarded when no strong reference to the value exists any more.

Changed in version 3.9:Added support for| and|= operators, as specified inPEP 584.

WeakValueDictionary objects have an additional method that has thesame issues as theWeakKeyDictionary.keyrefs() method.

WeakValueDictionary.valuerefs()

Return an iterable of the weak references to the values.

classweakref.WeakSet([elements])

Set class that keeps weak references to its elements. An element will bediscarded when no strong reference to it exists any more.

classweakref.WeakMethod(method[,callback])

A customref subclass which simulates a weak reference to a boundmethod (i.e., a method defined on a class and looked up on an instance).Since a bound method is ephemeral, a standard weak reference cannot keephold of it.WeakMethod has special code to recreate the boundmethod until either the object or the original function dies:

>>>classC:...defmethod(self):...print("method called!")...>>>c=C()>>>r=weakref.ref(c.method)>>>r()>>>r=weakref.WeakMethod(c.method)>>>r()<bound method C.method of <__main__.C object at 0x7fc859830220>>>>>r()()method called!>>>delc>>>gc.collect()0>>>r()>>>

callback is the same as the parameter of the same name to theref() function.

Added in version 3.4.

classweakref.finalize(obj,func,/,*args,**kwargs)

Return a callable finalizer object which will be called whenobjis garbage collected. Unlike an ordinary weak reference, a finalizerwill always survive until the reference object is collected, greatlysimplifying lifecycle management.

A finalizer is consideredalive until it is called (either explicitlyor at garbage collection), and after that it isdead. Calling a livefinalizer returns the result of evaluatingfunc(*arg,**kwargs),whereas calling a dead finalizer returnsNone.

Exceptions raised by finalizer callbacks during garbage collectionwill be shown on the standard error output, but cannot bepropagated. They are handled in the same way as exceptions raisedfrom an object’s__del__() method or a weak reference’scallback.

When the program exits, each remaining live finalizer is calledunless itsatexit attribute has been set to false. Theyare called in reverse order of creation.

A finalizer will never invoke its callback during the later part oftheinterpreter shutdown when module globals are liable to havebeen replaced byNone.

__call__()

Ifself is alive then mark it as dead and return the result ofcallingfunc(*args,**kwargs). Ifself is dead then returnNone.

detach()

Ifself is alive then mark it as dead and return the tuple(obj,func,args,kwargs). Ifself is dead then returnNone.

peek()

Ifself is alive then return the tuple(obj,func,args,kwargs). Ifself is dead then returnNone.

alive

Property which is true if the finalizer is alive, false otherwise.

atexit

A writable boolean property which by default is true. When theprogram exits, it calls all remaining live finalizers for whichatexit is true. They are called in reverse order ofcreation.

Note

It is important to ensure thatfunc,args andkwargs donot own any references toobj, either directly or indirectly,since otherwiseobj will never be garbage collected. Inparticular,func should not be a bound method ofobj.

Added in version 3.4.

weakref.ReferenceType

The type object for weak references objects.

weakref.ProxyType

The type object for proxies of objects which are not callable.

weakref.CallableProxyType

The type object for proxies of callable objects.

weakref.ProxyTypes

Sequence containing all the type objects for proxies. This can make it simplerto test if an object is a proxy without being dependent on naming both proxytypes.

See also

PEP 205 - Weak References

The proposal and rationale for this feature, including links to earlierimplementations and information about similar features in other languages.

Weak Reference Objects

Weak reference objects have no methods and no attributes besidesref.__callback__. A weak reference object allows the referent to beobtained, if it still exists, by calling it:

>>>importweakref>>>classObject:...pass...>>>o=Object()>>>r=weakref.ref(o)>>>o2=r()>>>oiso2True

If the referent no longer exists, calling the reference object returnsNone:

>>>delo,o2>>>print(r())None

Testing that a weak reference object is still live should be done using theexpressionref()isnotNone. Normally, application code that needs to usea reference object should follow this pattern:

# r is a weak reference objecto=r()ifoisNone:# referent has been garbage collectedprint("Object has been deallocated; can't frobnicate.")else:print("Object is still live!")o.do_something_useful()

Using a separate test for “liveness” creates race conditions in threadedapplications; another thread can cause a weak reference to become invalidatedbefore the weak reference is called; the idiom shown above is safe in threadedapplications as well as single-threaded applications.

Specialized versions ofref objects can be created through subclassing.This is used in the implementation of theWeakValueDictionary to reducethe memory overhead for each entry in the mapping. This may be most useful toassociate additional information with a reference, but could also be used toinsert additional processing on calls to retrieve the referent.

This example shows how a subclass ofref can be used to storeadditional information about an object and affect the value that’s returned whenthe referent is accessed:

importweakrefclassExtendedRef(weakref.ref):def__init__(self,ob,callback=None,/,**annotations):super().__init__(ob,callback)self.__counter=0fork,vinannotations.items():setattr(self,k,v)def__call__(self):"""Return a pair containing the referent and the number of        times the reference has been called.        """ob=super().__call__()ifobisnotNone:self.__counter+=1ob=(ob,self.__counter)returnob

Example

This simple example shows how an application can use object IDs to retrieveobjects that it has seen before. The IDs of the objects can then be used inother data structures without forcing the objects to remain alive, but theobjects can still be retrieved by ID if they do.

importweakref_id2obj_dict=weakref.WeakValueDictionary()defremember(obj):oid=id(obj)_id2obj_dict[oid]=objreturnoiddefid2obj(oid):return_id2obj_dict[oid]

Finalizer Objects

The main benefit of usingfinalize is that it makes it simpleto register a callback without needing to preserve the returned finalizerobject. For instance

>>>importweakref>>>classObject:...pass...>>>kenny=Object()>>>weakref.finalize(kenny,print,"You killed Kenny!")<finalize object at ...; for 'Object' at ...>>>>delkennyYou killed Kenny!

The finalizer can be called directly as well. However the finalizerwill invoke the callback at most once.

>>>defcallback(x,y,z):...print("CALLBACK")...returnx+y+z...>>>obj=Object()>>>f=weakref.finalize(obj,callback,1,2,z=3)>>>assertf.alive>>>assertf()==6CALLBACK>>>assertnotf.alive>>>f()# callback not called because finalizer dead>>>delobj# callback not called because finalizer dead

You can unregister a finalizer using itsdetach()method. This kills the finalizer and returns the arguments passed tothe constructor when it was created.

>>>obj=Object()>>>f=weakref.finalize(obj,callback,1,2,z=3)>>>f.detach()(<...Object object ...>, <function callback ...>, (1, 2), {'z': 3})>>>newobj,func,args,kwargs=_>>>assertnotf.alive>>>assertnewobjisobj>>>assertfunc(*args,**kwargs)==6CALLBACK

Unless you set theatexit attribute toFalse, a finalizer will be called when the program exits if itis still alive. For instance

>>>obj=Object()>>>weakref.finalize(obj,print,"obj dead or exiting")<finalize object at ...; for 'Object' at ...>>>>exit()obj dead or exiting

Comparing finalizers with__del__() methods

Suppose we want to create a class whose instances represent temporarydirectories. The directories should be deleted with their contentswhen the first of the following events occurs:

  • the object is garbage collected,

  • the object’sremove() method is called, or

  • the program exits.

We might try to implement the class using a__del__() method asfollows:

classTempDir:def__init__(self):self.name=tempfile.mkdtemp()defremove(self):ifself.nameisnotNone:shutil.rmtree(self.name)self.name=None@propertydefremoved(self):returnself.nameisNonedef__del__(self):self.remove()

Starting with Python 3.4,__del__() methods no longer preventreference cycles from being garbage collected, and module globals areno longer forced toNone duringinterpreter shutdown.So this code should work without any issues on CPython.

However, handling of__del__() methods is notoriously implementationspecific, since it depends on internal details of the interpreter’s garbagecollector implementation.

A more robust alternative can be to define a finalizer which only referencesthe specific functions and objects that it needs, rather than having accessto the full state of the object:

classTempDir:def__init__(self):self.name=tempfile.mkdtemp()self._finalizer=weakref.finalize(self,shutil.rmtree,self.name)defremove(self):self._finalizer()@propertydefremoved(self):returnnotself._finalizer.alive

Defined like this, our finalizer only receives a reference to the detailsit needs to clean up the directory appropriately. If the object never getsgarbage collected the finalizer will still be called at exit.

The other advantage of weakref based finalizers is that they can be used toregister finalizers for classes where the definition is controlled by athird party, such as running code when a module is unloaded:

importweakref,sysdefunloading_module():# implicit reference to the module globals from the function bodyweakref.finalize(sys.modules[__name__],unloading_module)

Note

If you create a finalizer object in a daemonic thread just as the programexits then there is the possibility that the finalizerdoes not get called at exit. However, in a daemonic threadatexit.register(),try:...finally:... andwith:...do not guarantee that cleanup occurs either.