Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-91081: Add note on WeakKeyDictionary behavior when deleting a replaced entry #91499

Merged
merged 9 commits into from
Dec 21, 2022
15 changes: 15 additions & 0 deletions Doc/library/weakref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,21 @@ See :ref:`__slots__ documentation <slots>` for details.
application without adding attributes to those objects. This can be especially
useful with objects that override attribute accesses.

Note that when a key with equal value to an existing key (but not equal identity)
is inserted into the dictionary, it replaces the value but does not replace the
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
...
>>> k1, k2 = T(), T()
>>> d = weakref.WeakKeyDictionary()
>>> d[k1] = 1 # d = {k1: 1}
slateny marked this conversation as resolved.
Show resolved Hide resolved
>>> d[k2] = 2 # d = {k1: 2}
>>> del k1 # d = {}


.. versionchanged:: 3.9
Added support for ``|`` and ``|=`` operators, specified in :pep:`584`.

Expand Down