File tree 1 file changed +24
-0
lines changed
1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -170,6 +170,30 @@ See :ref:`__slots__ documentation <slots>` for details.
170
170
application without adding attributes to those objects. This can be especially
171
171
useful with objects that override attribute accesses.
172
172
173
+ Note that when a key with equal value to an existing key (but not equal identity)
174
+ is inserted into the dictionary, it replaces the value but does not replace the
175
+ existing key. Due to this, when the reference to the original key is deleted, it
176
+ also deletes the entry in the dictionary::
177
+
178
+ >>> class T(str): pass
179
+ ...
180
+ >>> k1, k2 = T(), T()
181
+ >>> d = weakref.WeakKeyDictionary()
182
+ >>> d[k1] = 1 # d = {k1: 1}
183
+ >>> d[k2] = 2 # d = {k1: 2}
184
+ >>> del k1 # d = {}
185
+
186
+ A workaround would be to remove the key prior to reassignment::
187
+
188
+ >>> class T(str): pass
189
+ ...
190
+ >>> k1, k2 = T(), T()
191
+ >>> d = weakref.WeakKeyDictionary()
192
+ >>> d[k1] = 1 # d = {k1: 1}
193
+ >>> del d[k1]
194
+ >>> d[k2] = 2 # d = {k2: 2}
195
+ >>> del k1 # d = {k2: 2}
196
+
173
197
.. versionchanged :: 3.9
174
198
Added support for ``| `` and ``|= `` operators, specified in :pep: `584 `.
175
199
You can’t perform that action at this time.
0 commit comments