[issue24123] Python 2.7 Tutorial Conflicting behavior with WeakValueDictionary.

Jesse Bacon report at bugs.python.org
Mon May 4 01:17:56 CEST 2015


Jesse Bacon added the comment:

https://docs.python.org/2/tutorial/stdlib2.html
Section 11.6. Weak References

The example code below from the python tutorial suggests that the value of 'a' is not persistent when cast into a WeakValueDictionary as entry 'primary'

The value persisted in the dictionary after 'a' was deleted and garbage collection had been called.  I did not see a bug report for this already forgive me if there was one already put in.  

import weakref, gc
class A:
    def __init__(self, value):
        self.value = value
    def __repr__(self):
        return str(self.value)

a = A(10)                   # create a reference
d = weakref.WeakValueDictionary()
d['primary'] = a            # does not create a reference
d['primary']                # fetch the object if it is still alive

del a                       # remove the one reference
gc.collect()                # run garbage collection right away

d['primary']                # entry was automatically removed

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24123>
_______________________________________


More information about the Python-bugs-list mailing list