Skip to content

Commit 5efc270

Browse files
committed
Document the UserDict.popitem() override
Provide documentation for the ordering guarantees in UserDict.popitem() in the method's docstring and in the Python docs.
1 parent 6b558a4 commit 5efc270

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

Doc/library/collections.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,14 @@ attribute.
13461346
A real dictionary used to store the contents of the :class:`UserDict`
13471347
class.
13481348

1349+
:class:`!UserDict` instances also override the following method:
13491350

1351+
.. method:: popitem
1352+
1353+
Remove and return a ``(key, value)`` pair from the wrapped dictionary. Pairs are
1354+
returned in in the same order as :func:`.data.popitem`. (For the default
1355+
:meth:`dict.popitem`, this order is :abbr:`LIFO (last-in, first-out)`.) If the
1356+
dictionary is empty, raises a :exc:`KeyError`.
13501357

13511358
:class:`UserList` objects
13521359
-------------------------

Lib/collections/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,13 @@ def copy(self):
12571257
# This method has a default implementation in MutableMapping, but dict's
12581258
# equivalent is last-in, first-out instead of first-in, first-out.
12591259
def popitem(self):
1260+
"""Remove and return a (key, value) pair as a 2-tuple.
1261+
1262+
Removes pairs in the same order as the wrapped mapping's popitem()
1263+
method. For dict objects (the default), that order is last-in,
1264+
first-out (LIFO).
1265+
Raises KeyError if the UserDict is empty.
1266+
"""
12601267
return self.data.popitem()
12611268

12621269

0 commit comments

Comments
 (0)