Skip to content

Commit 55ac485

Browse files
committed
Clarify setdefault() behavior for dbm objects
1 parent d8a9466 commit 55ac485

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Doc/library/dbm.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ The object returned by :func:`~dbm.open` supports the same basic functionality a
9595
deleted, and the :keyword:`in` operator and the :meth:`!keys` method are
9696
available, as well as :meth:`!get` and :meth:`!setdefault` methods.
9797

98+
.. note::
99+
Unlike the standard :class:`dict` type, the :meth:`dict.setdefault` method
100+
for DBM objects requires both the *key* and *default* parameters.
101+
98102
Key and values are always stored as :class:`bytes`. This means that when
99103
strings are used they are implicitly converted to the default encoding before
100104
being stored.
@@ -204,6 +208,14 @@ or any other SQLite browser, including the SQLite CLI.
204208

205209
.. versionadded:: next
206210

211+
.. method:: sqlite3.setdefault(key, default)
212+
213+
If *key* is in the database, return its value. If not, insert *key* with
214+
a value of *default* and return *default*. Unlike :meth:`dict.setdefault`,
215+
both parameters are required. Calling ``setdefault(key)`` with only a key
216+
will raise :exc:`dbm.sqlite3.error`.
217+
218+
207219
:mod:`dbm.gnu` --- GNU database manager
208220
---------------------------------------
209221

@@ -323,6 +335,13 @@ functionality like crash tolerance.
323335

324336
.. versionadded:: 3.13
325337

338+
.. method:: gdbm.setdefault(key, default)
339+
340+
If *key* is in the database, return its value. If not, insert *key* with
341+
a value of *default* and return *default*. Unlike :meth:`dict.setdefault`,
342+
both parameters are required. Calling ``setdefault(key)`` with only a key
343+
will raise :exc:`TypeError`.
344+
326345

327346
:mod:`dbm.ndbm` --- New Database Manager
328347
----------------------------------------
@@ -400,6 +419,13 @@ This module can be used with the "classic" NDBM interface or the
400419

401420
.. versionadded:: 3.13
402421

422+
.. method:: ndbm.setdefault(key, default)
423+
424+
If *key* is in the database, return its value. If not, insert *key* with
425+
a value of *default* and return *default*. Unlike :meth:`dict.setdefault`,
426+
both parameters are required. Calling ``setdefault(key)`` with only a key
427+
will raise :exc:`_dbm.error`.
428+
403429

404430
:mod:`dbm.dumb` --- Portable DBM implementation
405431
-----------------------------------------------
@@ -501,3 +527,10 @@ The :mod:`!dbm.dumb` module defines the following:
501527
that this factor changes for each :mod:`dbm` submodule.
502528

503529
.. versionadded:: next
530+
531+
.. method:: dumbdbm.setdefault(key, default)
532+
533+
If *key* is in the database, return its value. If not, insert *key* with
534+
a value of *default* and return *default*. Unlike :meth:`dict.setdefault`,
535+
both parameters are required. Calling ``setdefault(key)`` with only a key
536+
will raise :exc:`TypeError`.

0 commit comments

Comments
 (0)