Issue33235
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) * ![]() |
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) * ![]() |
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) * ![]() |
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) * ![]() |
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:59 | admin | set | github: 77416 |
| 2020-10-03 18:22:46 | methane | set | status: open -> closed resolution: out of date stage: resolved |
| 2020-10-03 18:01:23 | iritkatriel | set | nosy:
+ iritkatriel messages: + msg377896 |
| 2018-04-06 16:43:41 | serhiy.storchaka | set | nosy:
+ serhiy.storchaka messages: + msg315024 |
| 2018-04-06 16:33:18 | eric.araujo | set | messages: + msg315022 |
| 2018-04-06 16:31:54 | eric.araujo | set | nosy:
+ eric.araujo messages: + msg315021 |
| 2018-04-06 10:13:47 | Paddy McCarthy | create | |
