3.3 weakref -- Weak references

New in version 2.1.

The weakref module allows the Python programmer to create weak references to objects.

XXX -- need to say more here!

Not all objects can be weakly referenced; those objects which do include class instances and dictionaries. Extension types can easily be made to support weak references; see section , ``Weak References in Extension Types,'' for more information.

Warning: The weak dictionaries provided in the current implementation and described below are subject to change. They are included to solicit feedback and usage experience, and may be changed or removed in the final version.

Warning: The desired semantics of weak-reference proxy objects are not completely clear; it is very difficult to create proxies which behave exactly like the type of the referent. The details of these objects are likely to change to some degree before the final release as experience reports become available.

Please send specific feeback on this module to Fred Drake at fdrake@acm.org.

ref (object[, callback])
Return a weak reference to object. If callback is provided, it 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. The original object can be retrieved by calling the reference object, if the referent is still alive.

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 propogated; they are handled in exactly the same way as exceptions raised from an object's __del__() method.

mapping ([dict])
Return a weak dictionary. If dict is given and not None, the new dictionary will contain the items contained in dict. The values from dict must be weakly referencable; if any values which would be inserted into the new mapping are not weakly referencable, TypeError will be raised and the new mapping will be empty.

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 prevent their use as dictionary keys. callable is the same as the parameter of the same name to the ref() function.

getweakrefcount (object)
Return the number of weak references and proxies which refer to object.

getweakrefs (object)
Return a list of all weak reference and proxy objects which refer to object.

WeakDictionary ([dict])
The class of the mapping objects returned by mapping(). This can be used for subclassing the implementation if needed.

ReferenceType
The type object for weak references objects.

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

CallableProxyType
The type object for proxies of callable objects.

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

See Also:

PEP 0205, Weak References
The proposal and rationale for this feature, including links to earlier implementations and information about similar features in other languages.


Subsections
See About this document... for information on suggesting changes.