Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add workaround example
  • Loading branch information
slateny committed Dec 10, 2022
commit 9f22bd7fcb1442d9180bafd2d1d40837c89a1813
14 changes: 12 additions & 2 deletions Doc/library/weakref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,25 @@ See :ref:`__slots__ documentation <slots>` for details.
existing key. Due to this, when the reference to the original key is deleted, it
also deletes the entry in the dictionary::

>>> class T(str):
... pass
>>> class T(str): pass
...
>>> k1, k2 = T(), T()
>>> d = weakref.WeakKeyDictionary()
>>> d[k1] = 1 # d = {k1: 1}
Comment thread
slateny marked this conversation as resolved.
>>> d[k2] = 2 # d = {k1: 2}
>>> del k1 # d = {}

A workaround would be to remove the key prior to reassignment::

>>> class T(str): pass
...
>>> k1, k2 = T(), T()
>>> d = weakref.WeakKeyDictionary()
>>> d[k1] = 1 # d = {k1: 1}
>>> d.pop(k1)
Comment thread
slateny marked this conversation as resolved.
Outdated
>>> d[k2] = 2 # d = {k2: 2}
>>> del k1 # d = {k2: 2}

Comment thread
slateny marked this conversation as resolved.
.. versionchanged:: 3.9
Added support for ``|`` and ``|=`` operators, specified in :pep:`584`.

Expand Down