Text of docs.python.org › id › 3 › library › weakref.html as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

weakref --- Weak references — Dokumentasi Python 3.15.2

Navigasi index modul | berikut | sebelum | Python » 3.15.2 documentation » The Python standard library » Tipe Data » weakref --- Weak references | Tema | weakref --- Weak references ¶ Source code: Lib/weakref.py The weakref module allows the Python programmer to create weak references to objects. In the following, the term referent means the object which is referred to by a weak reference. A weak reference to an object is not enough to keep the object alive: when the only remaining references to a referent are weak references, garbage collection is free to destroy the referent and reuse its memory for something else.

However, until the object is actually destroyed the weak reference may return the object even if there are no strong references to it. A primary use for weak references is to implement caches or mappings holding large objects, where it's desired that a large object not be kept 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 to associate a name with each.

If you used a Python dictionary to map names to images, or images to names, the image objects would remain alive just because they appeared as values or keys in the dictionaries. The WeakKeyDictionary and WeakValueDictionary classes supplied by the weakref module are an alternative, using weak references to construct mappings that don't keep objects alive solely because they appear in the mapping objects. If, for example, an image object is a value in a WeakValueDictionary , then when the last remaining references to that image object are the weak references held by weak mappings, garbage collection can reclaim the object, and its corresponding entries in weak mappings are simply deleted.

WeakKeyDictionary and WeakValueDictionary use weak references in their implementation, setting up callback functions on the weak references that notify the weak dictionaries when a key or value has been reclaimed by garbage collection. WeakSet implements the set interface, but keeps weak references to its elements, just like a WeakKeyDictionary does. finalize provides a straight forward way to register a cleanup function to be called when an object is garbage collected.

This is simpler to use than setting up a callback function on a raw weak reference, since the module automatically ensures that the finalizer remains alive until the object is collected. Most programs should find that using one of these weak container types or finalize is all they need -- it's not usually necessary to create your own weak references directly. The low-level machinery is exposed by the weakref module for the benefit of advanced uses.

Not all objects can be weakly referenced. Objects which support weak references include class instances, functions written in Python (but not in C), instance methods, sets, frozensets, some file objects , generators , type objects, sockets, arrays, deques, regular expression pattern objects, and code objects. Berubah pada versi 3.2: Added support for thread.lock, threading.Lock, and code objects. Several built-in types such as list and dict do not directly support weak references but can add support through subclassing: class Dict ( dict ): pass obj = Dict ( red = 1 , green = 2 , blue = 3 ) # this object is weak referenceable Detail implementasi CPython: Other built-in types such as tuple and int do not support weak references even when subclassed.

Extension types can easily be made to support weak references; see Weak Reference Support . When __slots__ are defined for a given type, weak reference support is disabled unless a '__weakref__' string is also present in the sequence of strings in the __slots__ declaration. See __slots__ documentation for details. class weakref. ref ( object [ , callback ] ) ¶ Return a weak reference to object . The original object can be retrieved by calling the reference object if the referent is still alive; if the referent is no longer alive, calling the reference object will cause None to be returned.

If callback is provided and not None , and the returned weakref object is still alive, the callback will be called when the object is about to be finalized; the weak reference object will be passed as the only parameter 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 most recently 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 exceptions raised from an object's __del__() method. Weak references are hashable if the object is hashable. They will maintain their hash value even after the object was deleted. If hash() is called the first time only after the object was deleted, the call will raise TypeError .

Weak references support tests for equality, but not ordering. If the referents are still alive, two references have the same equality relationship as their referents (regardless of the callback ). 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. Weak references are generic over the type of the object they reference. __callback__ ¶ This read-only attribute returns the callback currently associated to the weakref.

If there is no callback or if the referent of the weakref is no longer alive then this attribute will have value None . Berubah pada versi 3.4: Added the __callback__ attribute. weakref. proxy ( object [ , callback ] ) ¶ Return a proxy to object which uses a weak reference. This supports use of the proxy in most contexts instead of requiring the explicit dereferencing used with weak reference objects. The returned object will have a type of either ProxyType or CallableProxyType , depending on whether object is callable.

Proxy objects are not hashable regardless of the referent; this avoids a number of problems related to their fundamentally mutable nature, and prevents their use as dictionary keys. callback is the same as the parameter of the same name to the ref() function. Accessing an attribute of the proxy object after the referent is garbage collected raises ReferenceError . Berubah pada versi 3.8: Extended the operator support on proxy objects to include the matrix multiplication operators @ and @= . weakref. getweakrefcount ( object ) ¶ Return the number of weak references and proxies which refer to object . weakref. getweakrefs ( object ) ¶ Return a list of all weak reference and proxy objects which refer to object . class weakref.

WeakKeyDictionary ( [ dict ] ) ¶ Mapping class that references keys weakly. Entries in the dictionary will be discarded when there is no longer a strong reference to the key. This can be used to associate additional data with an object owned by other parts of an application without adding attributes to those objects. This can be especially useful 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 the existing key.

Due to this, when the reference to the original key is deleted, it also deletes the entry in the dictionary: >>> class T ( str ): pass ... >>> k1 , k2 = T (), T () >>> d = weakref . WeakKeyDictionary () >>> d [ k1 ] = 1 # d = {k1: 1} >>> d [ k2 ] = 2 # d = {k1: 2} >>> del k1 # d = {} A workaround would be to remove the key prior to reassignment: >>> class T ( str ): pass ... >>> k1 , k2 = T (), T () >>> d = weakref . WeakKeyDictionary () >>> d [ k1 ] = 1 # d = {k1: 1} >>> del d [ k1 ] >>> d [ k2 ] = 2 # d = {k2: 2} >>> del k1 # d = {k2: 2} Berubah pada versi 3.9: Added support for | and |= operators, as specified in PEP 584 .

WeakKey

The rest of this page is on the live site.