WeakValueDictionary

B

Bryan

Bryan said:
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 said:
del a
d.items()

thanks,

bryan


i have another question about weak references. how do you know that b is dead?

<weakref at 011739F0; dead>
 
J

Just

Bryan <[email protected]> said:
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 said:
del a
d.items()

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 said:
>>> del a
>>> d.items()
[(1 said:
[(1 said:
>>> 12 12
>>> d.items() []
>>>

Just
 
A

Andrew Dalke

Bryan said:
i have another question about weak references. how do you know that b
is dead?


<weakref at 011739F0; dead>

Quoting from the weakref PEP at
http://www.python.org/peps/pep-0205.html

A weak reference object will allow access to the referenced object
if it hasn't been collected and to determine if the object still
exists in memory. Retrieving the referent is done by calling the
reference object. If the referent is no longer alive, this will
return None instead.
.... def hello(self): print "Hello!"
....Traceback (most recent call last):

Andrew
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top