GNU Info

Info Node: (python2.1-lib.info)Example

(python2.1-lib.info)Example


Next: Weak References in Extension Types Prev: Weak Reference Objects Up: weakref
Enter node , (file) or (file)node

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.

     import weakref
     
     _id2obj_dict = weakref.WeakValueDictionary()
     
     def remember(obj):
         _id2obj_dict[id(obj)] = obj
     
     def id2obj(id):
         return _id2obj_dict.get(id)


automatically generated by info2www version 1.2.2.9