File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -181,6 +181,28 @@ \subsection{Weak Reference Objects
181181single-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
You can’t perform that action at this time.
0 commit comments