File tree Expand file tree Collapse file tree 1 file changed +12
-2
lines changed Expand file tree Collapse file tree 1 file changed +12
-2
lines changed Original file line number Diff line number Diff line change @@ -172,15 +172,25 @@ See :ref:`__slots__ documentation <slots>` for details.
172
172
existing key. Due to this, when the reference to the original key is deleted, it
173
173
also deletes the entry in the dictionary::
174
174
175
- >>> class T(str):
176
- ... pass
175
+ >>> class T(str): pass
177
176
...
178
177
>>> k1, k2 = T(), T()
179
178
>>> d = weakref.WeakKeyDictionary()
180
179
>>> d[k1] = 1 # d = {k1: 1}
181
180
>>> d[k2] = 2 # d = {k1: 2}
182
181
>>> del k1 # d = {}
183
182
183
+ A workaround would be to remove the key prior to reassignment::
184
+
185
+ >>> class T(str): pass
186
+ ...
187
+ >>> k1, k2 = T(), T()
188
+ >>> d = weakref.WeakKeyDictionary()
189
+ >>> d[k1] = 1 # d = {k1: 1}
190
+ >>> d.pop(k1)
191
+ >>> d[k2] = 2 # d = {k2: 2}
192
+ >>> del k1 # d = {k2: 2}
193
+
184
194
.. versionchanged :: 3.9
185
195
Added support for ``| `` and ``|= `` operators, specified in :pep: `584 `.
186
196
You can’t perform that action at this time.
0 commit comments