Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cb83988

Browse files
committed
Added example use of weak references, contributed by Tim Peters.
1 parent 58c8f9f commit cb83988

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

Doc/lib/libweakref.tex

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,28 @@ \subsection{Weak Reference Objects
181181
single-threaded applications.
182182

183183

184+
\subsection{Example \label{weakref-example}}
185+
186+
This simple example shows how an application can use objects IDs to
187+
retrieve objects that it has seen before. The IDs of the objects can
188+
then be used in other data structures without forcing the objects to
189+
remain alive, but the objects can still be retrieved by ID if they
190+
do.
191+
192+
% Example contributed by Tim Peters <[email protected]>.
193+
\begin{verbatim}
194+
import weakref
195+
196+
_id2obj_dict = weakref.mapping()
197+
198+
def remember(obj):
199+
_id2obj_dict[id(obj)] = obj
200+
201+
def id2obj(id):
202+
return _id2obj_dict.get(id)
203+
\end{verbatim}
204+
205+
184206
\subsection{Weak References in Extension Types
185207
\label{weakref-extension}}
186208

0 commit comments

Comments
 (0)