summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFred Drake <fdrake@acm.org>2001-03-28 21:15:41 (GMT)
committerFred Drake <fdrake@acm.org>2001-03-28 21:15:41 (GMT)
commitcb8398815150e458b3f977a5bd398e576d325aa3 (patch)
tree5bb354881575d7eb25056b46c7a499fd7545090b
parent58c8f9f631090a2883744c07dce407eb87c265d3 (diff)
downloadcpython-cb8398815150e458b3f977a5bd398e576d325aa3.zip
cpython-cb8398815150e458b3f977a5bd398e576d325aa3.tar.gz
cpython-cb8398815150e458b3f977a5bd398e576d325aa3.tar.bz2
Added example use of weak references, contributed by Tim Peters.
-rw-r--r--Doc/lib/libweakref.tex22
1 files changed, 22 insertions, 0 deletions
diff --git a/Doc/lib/libweakref.tex b/Doc/lib/libweakref.tex
index 07e6a3b..ca6c08a 100644
--- a/Doc/lib/libweakref.tex
+++ b/Doc/lib/libweakref.tex
@@ -181,6 +181,28 @@ idiom shown above is safe in threaded applications as well as
single-threaded applications.
+\subsection{Example \label{weakref-example}}
+
+This simple example shows how an application can use objects IDs to
+retrieve objects that it has seen before. The IDs of the objects can
+then be used in other data structures without forcing the objects to
+remain alive, but the objects can still be retrieved by ID if they
+do.
+
+% Example contributed by Tim Peters <tim_one@msn.com>.
+\begin{verbatim}
+import weakref
+
+_id2obj_dict = weakref.mapping()
+
+def remember(obj):
+ _id2obj_dict[id(obj)] = obj
+
+def id2obj(id):
+ return _id2obj_dict.get(id)
+\end{verbatim}
+
+
\subsection{Weak References in Extension Types
\label{weakref-extension}}