[Python-checkins] CVS: python/dist/src/Doc/lib libweakref.tex,1.4,1.5

Fred L. Drake fdrake@users.sourceforge.net
Wed, 28 Mar 2001 13:15:44 -0800


Update of /cvsroot/python/python/dist/src/Doc/lib
In directory usw-pr-cvs1:/tmp/cvs-serv16482/lib

Modified Files:
	libweakref.tex 
Log Message:

Added example use of weak references, contributed by Tim Peters.


Index: libweakref.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/lib/libweakref.tex,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -r1.4 -r1.5
*** libweakref.tex	2001/03/23 04:36:02	1.4
--- libweakref.tex	2001/03/28 21:15:41	1.5
***************
*** 182,185 ****
--- 182,207 ----
  
  
+ \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}}