WeakValueDictionary

Just just at xs4all.nl
Sun Oct 3 02:03:19 EDT 2004


In article <VEK7d.58507$He1.41678 at attbi_s01>, Bryan <belred1 at yahoo.com> 
wrote:

> there must be something i don't understand about WeakKeyDictionary and 
> WeakValueDictionary.  i had expected the last 
> line to be empty because i thought i deleted the last strong reference to 
> variable "a". what am i misunderstanding?
> 
>  >>> d = weakref.WeakValueDictionary()
>  >>> a = A()
>  >>> d[1] = a
>  >>> d.items()
> [(1, <__main__.A instance at 0x0116BC60>)]
>  >>> del a
>  >>> d.items()
> [(1, <__main__.A instance at 0x0116BC60>)]
>  >>>

There's a reference left in the magic _ variable:

  >>> import weakref
  >>> class A: pass
  ... 
  >>> d = weakref.WeakValueDictionary()
  >>> a = A()
  >>> d[1] = a
  >>> d.items()
  [(1, <__main__.A instance at 0x115490>)]
  >>> del a
  >>> d.items()
  [(1, <__main__.A instance at 0x115490>)]
  >>> _
  [(1, <__main__.A instance at 0x115490>)]
  >>> 12
  12
  >>> d.items()
  []
  >>> 

Just



More information about the Python-list mailing list