[issue33235] Better help text for dict.setdefault
New submission from Paddy McCarthy <paddy3118@gmail.com>: 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) ---------- assignee: docs@python components: Documentation messages: 315015 nosy: Paddy McCarthy, docs@python priority: normal severity: normal status: open title: Better help text for dict.setdefault type: enhancement versions: Python 3.6 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue33235> _______________________________________
Éric Araujo <merwok@netwok.org> added the comment: 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. ---------- nosy: +eric.araujo _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue33235> _______________________________________
Éric Araujo <merwok@netwok.org> added the comment: 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. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue33235> _______________________________________
Serhiy Storchaka <storchaka+cpython@gmail.com> added the comment: 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. ---------- nosy: +serhiy.storchaka _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue33235> _______________________________________
Irit Katriel <iritkatriel@yahoo.com> added the comment: 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. ---------- nosy: +iritkatriel _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue33235> _______________________________________
Change by Inada Naoki <songofacandy@gmail.com>: ---------- resolution: -> out of date stage: -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue33235> _______________________________________
participants (5)
-
Inada Naoki
-
Irit Katriel
-
Paddy McCarthy
-
Serhiy Storchaka
-
Éric Araujo