This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Better help text for dict.setdefault
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.6
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Paddy McCarthy, docs@python, eric.araujo, iritkatriel, serhiy.storchaka
Priority: normal Keywords:

Created on 2018-04-06 10:13 by Paddy McCarthy, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg315015 - (view) Author: Paddy McCarthy (Paddy McCarthy) Date: 2018-04-06 10:13
Hi, I was answering some question and used dict.setdefault as part of the solution and posted the help() on it as part of my answer.

The help was this:

In [15]: help(mapper.setdefault)
Help on built-in function setdefault:

setdefault(...) method of builtins.dict instance
    D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D

This seems the wrong way around. Is it not better expressed as

    D.setdefault(k[,d]) -> set D[k]=d if k not in D and then D.get(k,d)
msg315021 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2018-04-06 16:31
The two lines are equivalent!

`d.setdefault(key, default)` does return the same thing as `d.get(key, default)`, and then sets `d[key] = default` if the get method went into the “key was not found, let’s return default” branch.
msg315022 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2018-04-06 16:33
Note that if we switch the order like you propose, we don’t need to use dict.get:


  set D[k]=d if k not in D then return D[k]


I suspect the current doc was written the way it is because it matches the actual implementation.
msg315024 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-04-06 16:43
Changing docstring can break existing code, therefore it should go only in future new Python version unless it contains a bug. The current docstring in 3.6 doesn't look incorrect to me. The docstring in 3.7 is different.
msg377896 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-10-03 18:01
The help message is now:

  Help on method_descriptor:
  setdefault(self, key, default=None, /)
    Insert key with a value of default if key is not in the dictionary.    
    Return the value for key if key is in the dictionary, else default.

This issue should be closed as out of date.
History
Date User Action Args
2022-04-11 14:58:59adminsetgithub: 77416
2020-10-03 18:22:46methanesetstatus: open -> closed
resolution: out of date
stage: resolved
2020-10-03 18:01:23iritkatrielsetnosy: + iritkatriel
messages: + msg377896
2018-04-06 16:43:41serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg315024
2018-04-06 16:33:18eric.araujosetmessages: + msg315022
2018-04-06 16:31:54eric.araujosetnosy: + eric.araujo
messages: + msg315021
2018-04-06 10:13:47Paddy McCarthycreate