Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / html / python / lib / module-weakref.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-fpectl.html" />
<link rel="prev" href="module-gc.html" />
<link rel="parent" href="python.html" />
<link rel="next" href="weakref-objects.html" />
<meta name='aesop' content='information' />
<title>3.3 weakref -- Weak references</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="3.2 gc "
href="module-gc.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="3. Python Runtime Services"
href="python.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="3.3.1 Weak Reference Objects"
href="weakref-objects.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-gc.html">3.2 gc </A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="weakref-objects.html">3.3.1 Weak Reference Objects</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION005300000000000000000">
3.3 <tt class="module">weakref</tt> --
Weak references</A>
</H1>
<P>
<A NAME="module-weakref"></A>
<P>
<span class="versionnote">New in version 2.1.</span>
<P>
The <tt class="module">weakref</tt> module allows the Python programmer to create
<i class="dfn">weak references</i> to objects.
<P>
In the following, the term <i class="dfn">referent</i> means the
object which is referred to by a weak reference.
<P>
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. 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 <tt class="class">WeakKeyDictionary</tt> and
<tt class="class">WeakValueDictionary</tt> classes supplied by the <tt class="module">weakref</tt>
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
<tt class="class">WeakValueDictionary</tt>, 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.
<P>
<tt class="class">WeakKeyDictionary</tt> and <tt class="class">WeakValueDictionary</tt> 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. Most programs should find that
using one of these weak dictionary types is all they need - it's
not usually necessary to create your own weak references directly. The
low-level machinery used by the weak dictionary implementations is exposed
by the <tt class="module">weakref</tt> module for the benefit of advanced uses.
<P>
Not all objects can be weakly referenced; those objects which can
include class instances, functions written in Python (but not in C),
methods (both bound and unbound), sets, frozensets, file objects,
generators, type objects, DBcursor objects from the <tt class="module">bsddb</tt> module,
sockets, arrays, deques, and regular expression pattern objects.
<span class="versionnote">Changed in version 2.4:
Added support for files, sockets, arrays, and patterns.</span>
<P>
Several builtin types such as <tt class="class">list</tt> and <tt class="class">dict</tt> do not
directly support weak references but can add support through subclassing:
<P>
<div class="verbatim"><pre>
class Dict(dict):
pass
obj = Dict(red=1, green=2, blue=3) # this object is weak referencable
</pre></div>
<P>
Extension types can easily be made to support weak references; see section
<A href="weakref-extension.html#weakref-extension">3.3.3</A>, ``Weak References in Extension Types,'' for more
information.
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-419' xml:id='l2h-419' class="class">ref</tt></b>(</nobr></td>
<td><var>object</var><big>[</big><var>, callback</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Return a weak reference to <var>object</var>. 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 <tt class="constant">None</tt> to be returned. If <var>callback</var> is
provided and not <tt class="constant">None</tt>,
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.
<P>
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.
<P>
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
<tt class="method">__del__()</tt> method.
<P>
Weak references are hashable if the <var>object</var> is hashable. They
will maintain their hash value even after the <var>object</var> was
deleted. If <tt class="function">hash()</tt> is called the first time only after
the <var>object</var> was deleted, the call will raise
<tt class="exception">TypeError</tt>.
<P>
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
<var>callback</var>). If either referent has been deleted, the
references are equal only if the reference objects are the same
object.
<P>
<span class="versionnote">Changed in version 2.4:
This is now a subclassable type rather than a
factory function; it derives from <tt class="class">object</tt>.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-420' xml:id='l2h-420' class="function">proxy</tt></b>(</nobr></td>
<td><var>object</var><big>[</big><var>, callback</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Return a proxy to <var>object</var> 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 <code>ProxyType</code> or
<code>CallableProxyType</code>, depending on whether <var>object</var> 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. <var>callback</var> is the same as the parameter of the same name
to the <tt class="function">ref()</tt> function.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-421' xml:id='l2h-421' class="function">getweakrefcount</tt></b>(</nobr></td>
<td><var>object</var>)</td></tr></table></dt>
<dd>
Return the number of weak references and proxies which refer to
<var>object</var>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-422' xml:id='l2h-422' class="function">getweakrefs</tt></b>(</nobr></td>
<td><var>object</var>)</td></tr></table></dt>
<dd>
Return a list of all weak reference and proxy objects which refer to
<var>object</var>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-423' xml:id='l2h-423' class="class">WeakKeyDictionary</tt></b>(</nobr></td>
<td><var></var><big>[</big><var>dict</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
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.
<P>
<span class="note"><b class="label">Note:</b>
Caution: Because a <tt class="class">WeakKeyDictionary</tt> is built on top
of a Python dictionary, it must not change size when iterating
over it. This can be difficult to ensure for a
<tt class="class">WeakKeyDictionary</tt> because actions performed by the
program during iteration may cause items in the dictionary
to vanish "by magic" (as a side effect of garbage collection).</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-424' xml:id='l2h-424' class="class">WeakValueDictionary</tt></b>(</nobr></td>
<td><var></var><big>[</big><var>dict</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Mapping class that references values weakly. Entries in the
dictionary will be discarded when no strong reference to the value
exists any more.
<P>
<span class="note"><b class="label">Note:</b>
Caution: Because a <tt class="class">WeakValueDictionary</tt> is built on top
of a Python dictionary, it must not change size when iterating
over it. This can be difficult to ensure for a
<tt class="class">WeakValueDictionary</tt> because actions performed by the
program during iteration may cause items in the dictionary
to vanish "by magic" (as a side effect of garbage collection).</span>
</dl>
<P>
<dl><dt><b><tt id='l2h-425' xml:id='l2h-425'>ReferenceType</tt></b></dt>
<dd>
The type object for weak references objects.
</dd></dl>
<P>
<dl><dt><b><tt id='l2h-426' xml:id='l2h-426'>ProxyType</tt></b></dt>
<dd>
The type object for proxies of objects which are not callable.
</dd></dl>
<P>
<dl><dt><b><tt id='l2h-427' xml:id='l2h-427'>CallableProxyType</tt></b></dt>
<dd>
The type object for proxies of callable objects.
</dd></dl>
<P>
<dl><dt><b><tt id='l2h-428' xml:id='l2h-428'>ProxyTypes</tt></b></dt>
<dd>
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.
</dd></dl>
<P>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-429' xml:id='l2h-429' class="exception">ReferenceError</tt></b></dt>
<dd>
Exception raised when a proxy object is used but the underlying
object has been collected. This is the same as the standard
<tt class="exception">ReferenceError</tt> exception.
</dd></dl>
<P>
<div class="seealso">
<p class="heading">See Also:</p>
<dl compact="compact" class="seerfc">
<dt><a href="http://www.python.org/peps/pep-0205.html"
title="Weak References"
>PEP 0205, <em>Weak References</em></a>
<dd>The proposal and rationale for this
feature, including links to earlier implementations
and information about similar features in other
languages.
</dl>
</div>
<P>
<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>
<UL CLASS="ChildLinks">
<LI><A href="weakref-objects.html">3.3.1 Weak Reference Objects</a>
<LI><A href="weakref-example.html">3.3.2 Example</a>
<LI><A href="weakref-extension.html">3.3.3 Weak References in Extension Types</a>
</ul>
<!--End of Table of Child-Links-->
</div>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="3.2 gc "
href="module-gc.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="3. Python Runtime Services"
href="python.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="3.3.1 Weak Reference Objects"
href="weakref-objects.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-gc.html">3.2 gc </A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="weakref-objects.html">3.3.1 Weak Reference Objects</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>