From 6c9d21720bb48fbb7f5f3472faebda1902af17f3 Mon Sep 17 00:00:00 2001 From: Yzi-Li <1538321957@qq.com> Date: Mon, 5 May 2025 15:53:30 +0800 Subject: [PATCH 01/11] move the explanation of dict equal before its use --- Doc/library/stdtypes.rst | 44 ++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 39aaa5da0786f8..094e67808cb414 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -4838,6 +4838,28 @@ can be used interchangeably to index the same dictionary entry. Providing keyword arguments as in the first example only works for keys that are valid Python identifiers. Otherwise, any valid keys can be used. + Dictionaries preserve insertion order. Note that updating a key does not + affect the order. Keys added after deletion are inserted at the end. :: + + >>> d = {"one": 1, "two": 2, "three": 3, "four": 4} + >>> d + {'one': 1, 'two': 2, 'three': 3, 'four': 4} + >>> list(d) + ['one', 'two', 'three', 'four'] + >>> list(d.values()) + [1, 2, 3, 4] + >>> d["one"] = 42 + >>> d + {'one': 42, 'two': 2, 'three': 3, 'four': 4} + >>> del d["two"] + >>> d["two"] = None + >>> d + {'one': 42, 'three': 3, 'four': 4, 'two': None} + + .. versionchanged:: 3.7 + Dictionary order is guaranteed to be insertion order. This behavior was + an implementation detail of CPython from 3.6. + These are the operations that dictionaries support (and therefore, custom mapping types should support too): @@ -5012,28 +5034,6 @@ can be used interchangeably to index the same dictionary entry. value)`` pairs (regardless of ordering). Order comparisons ('<', '<=', '>=', '>') raise :exc:`TypeError`. - Dictionaries preserve insertion order. Note that updating a key does not - affect the order. Keys added after deletion are inserted at the end. :: - - >>> d = {"one": 1, "two": 2, "three": 3, "four": 4} - >>> d - {'one': 1, 'two': 2, 'three': 3, 'four': 4} - >>> list(d) - ['one', 'two', 'three', 'four'] - >>> list(d.values()) - [1, 2, 3, 4] - >>> d["one"] = 42 - >>> d - {'one': 42, 'two': 2, 'three': 3, 'four': 4} - >>> del d["two"] - >>> d["two"] = None - >>> d - {'one': 42, 'three': 3, 'four': 4, 'two': None} - - .. versionchanged:: 3.7 - Dictionary order is guaranteed to be insertion order. This behavior was - an implementation detail of CPython from 3.6. - Dictionaries and dictionary views are reversible. :: >>> d = {"one": 1, "two": 2, "three": 3, "four": 4} From 17dbd2b19c740c4fdb3611f66b34e0d6ee3ae779 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Tue, 6 May 2025 00:03:50 -0400 Subject: [PATCH 02/11] Update Doc/library/stdtypes.rst --- Doc/library/stdtypes.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 094e67808cb414..a16a4a50182369 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -4860,7 +4860,6 @@ can be used interchangeably to index the same dictionary entry. Dictionary order is guaranteed to be insertion order. This behavior was an implementation detail of CPython from 3.6. - These are the operations that dictionaries support (and therefore, custom mapping types should support too): From 0600d30567e91398de32948845dc161ea5515a8f Mon Sep 17 00:00:00 2001 From: Yzi-Li <1538321957@qq.com> Date: Tue, 6 May 2025 12:24:45 +0800 Subject: [PATCH 03/11] reposition the explanation --- Doc/library/stdtypes.rst | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index a16a4a50182369..1956d5fb6b906f 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -4823,7 +4823,11 @@ can be used interchangeably to index the same dictionary entry. being added is already present, the value from the keyword argument replaces the value from the positional argument. - To illustrate, the following examples all return a dictionary equal to + Providing keyword arguments as in the first example only works for keys that + are valid Python identifiers. Otherwise, any valid keys can be used. + + To illustrate dictionary creation and equality, + the following examples all return a dictionary equal to ``{"one": 1, "two": 2, "three": 3}``:: >>> a = dict(one=1, two=2, three=3) @@ -4835,9 +4839,6 @@ can be used interchangeably to index the same dictionary entry. >>> a == b == c == d == e == f True - Providing keyword arguments as in the first example only works for keys that - are valid Python identifiers. Otherwise, any valid keys can be used. - Dictionaries preserve insertion order. Note that updating a key does not affect the order. Keys added after deletion are inserted at the end. :: From b6b3a83cefa97e8291508e2dfd55b09acd1148d5 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Wed, 7 May 2025 00:08:02 -0400 Subject: [PATCH 04/11] Update Doc/library/stdtypes.rst --- Doc/library/stdtypes.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 1956d5fb6b906f..0c20e8c18daf65 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -4824,9 +4824,7 @@ can be used interchangeably to index the same dictionary entry. replaces the value from the positional argument. Providing keyword arguments as in the first example only works for keys that - are valid Python identifiers. Otherwise, any valid keys can be used. - - To illustrate dictionary creation and equality, + are valid Python identifiers. Otherwise, any valid keys can be used. To illustrate dictionary creation and equality, the following examples all return a dictionary equal to ``{"one": 1, "two": 2, "three": 3}``:: From a5880ced871b389d1478a1ef365e9e612df9495a Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Wed, 7 May 2025 00:13:03 -0400 Subject: [PATCH 05/11] Update Doc/library/stdtypes.rst --- Doc/library/stdtypes.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 0c20e8c18daf65..cade5fc133a4fa 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -4824,7 +4824,9 @@ can be used interchangeably to index the same dictionary entry. replaces the value from the positional argument. Providing keyword arguments as in the first example only works for keys that - are valid Python identifiers. Otherwise, any valid keys can be used. To illustrate dictionary creation and equality, + are valid Python identifiers. Otherwise, any valid keys can be used. + + To illustrate dictionary creation and equality, the following examples all return a dictionary equal to ``{"one": 1, "two": 2, "three": 3}``:: From bf6728072ee1709de64dd9313e2faac200319642 Mon Sep 17 00:00:00 2001 From: Yzi-Li <1538321957@qq.com> Date: Wed, 7 May 2025 19:01:54 +0800 Subject: [PATCH 06/11] Revert "reposition the explanation" This reverts commit 0600d30567e91398de32948845dc161ea5515a8f. --- Doc/library/stdtypes.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index cade5fc133a4fa..e0ec98183b537f 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -4823,11 +4823,15 @@ can be used interchangeably to index the same dictionary entry. being added is already present, the value from the keyword argument replaces the value from the positional argument. +<<<<<<< HEAD Providing keyword arguments as in the first example only works for keys that are valid Python identifiers. Otherwise, any valid keys can be used. To illustrate dictionary creation and equality, the following examples all return a dictionary equal to +======= + To illustrate, the following examples all return a dictionary equal to +>>>>>>> parent of 0600d30567e (reposition the explanation) ``{"one": 1, "two": 2, "three": 3}``:: >>> a = dict(one=1, two=2, three=3) @@ -4839,6 +4843,9 @@ can be used interchangeably to index the same dictionary entry. >>> a == b == c == d == e == f True + Providing keyword arguments as in the first example only works for keys that + are valid Python identifiers. Otherwise, any valid keys can be used. + Dictionaries preserve insertion order. Note that updating a key does not affect the order. Keys added after deletion are inserted at the end. :: From e311ff02266ca0aec0f41bc9bab4248547f4b7d3 Mon Sep 17 00:00:00 2001 From: Yzi-Li <1538321957@qq.com> Date: Wed, 7 May 2025 19:18:50 +0800 Subject: [PATCH 07/11] move dict equal explanation --- Doc/library/stdtypes.rst | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index e0ec98183b537f..5c1d0b3c89a6c3 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -4823,15 +4823,13 @@ can be used interchangeably to index the same dictionary entry. being added is already present, the value from the keyword argument replaces the value from the positional argument. -<<<<<<< HEAD Providing keyword arguments as in the first example only works for keys that are valid Python identifiers. Otherwise, any valid keys can be used. - To illustrate dictionary creation and equality, + Dictionaries compare equal if and only if they have the same ``(key, + value)`` pairs (regardless of ordering). Order comparisons ('<', '<=', '>=', '>') raise + :exc:`TypeError`. To illustrate dictionary creation and equality, the following examples all return a dictionary equal to -======= - To illustrate, the following examples all return a dictionary equal to ->>>>>>> parent of 0600d30567e (reposition the explanation) ``{"one": 1, "two": 2, "three": 3}``:: >>> a = dict(one=1, two=2, three=3) @@ -5037,10 +5035,6 @@ can be used interchangeably to index the same dictionary entry. .. versionadded:: 3.9 - Dictionaries compare equal if and only if they have the same ``(key, - value)`` pairs (regardless of ordering). Order comparisons ('<', '<=', '>=', '>') raise - :exc:`TypeError`. - Dictionaries and dictionary views are reversible. :: >>> d = {"one": 1, "two": 2, "three": 3, "four": 4} From ede963368e085e0a4a55a8a25d1080577d715617 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Wed, 7 May 2025 17:49:07 -0400 Subject: [PATCH 08/11] Remove trailing space --- Doc/library/stdtypes.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 5c1d0b3c89a6c3..fa0c22cae362a4 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -4825,7 +4825,6 @@ can be used interchangeably to index the same dictionary entry. Providing keyword arguments as in the first example only works for keys that are valid Python identifiers. Otherwise, any valid keys can be used. - Dictionaries compare equal if and only if they have the same ``(key, value)`` pairs (regardless of ordering). Order comparisons ('<', '<=', '>=', '>') raise :exc:`TypeError`. To illustrate dictionary creation and equality, From 9446a11ddb3cf89550f8451e4b470b069be76d69 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Wed, 7 May 2025 17:50:01 -0400 Subject: [PATCH 09/11] Add back newline --- Doc/library/stdtypes.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index fa0c22cae362a4..5c1d0b3c89a6c3 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -4825,6 +4825,7 @@ can be used interchangeably to index the same dictionary entry. Providing keyword arguments as in the first example only works for keys that are valid Python identifiers. Otherwise, any valid keys can be used. + Dictionaries compare equal if and only if they have the same ``(key, value)`` pairs (regardless of ordering). Order comparisons ('<', '<=', '>=', '>') raise :exc:`TypeError`. To illustrate dictionary creation and equality, From bbdaea9ad5e69088e9292cb3e54cdd88b0c74a64 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Wed, 7 May 2025 17:56:24 -0400 Subject: [PATCH 10/11] Properly remove trailing spaces --- Doc/library/stdtypes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 5c1d0b3c89a6c3..61d39a6671caed 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -4825,7 +4825,7 @@ can be used interchangeably to index the same dictionary entry. Providing keyword arguments as in the first example only works for keys that are valid Python identifiers. Otherwise, any valid keys can be used. - + Dictionaries compare equal if and only if they have the same ``(key, value)`` pairs (regardless of ordering). Order comparisons ('<', '<=', '>=', '>') raise :exc:`TypeError`. To illustrate dictionary creation and equality, From fb5179c5f668e6a1f05e96eb14fc6f6f6b60787c Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Wed, 7 May 2025 17:57:12 -0400 Subject: [PATCH 11/11] Update Doc/library/stdtypes.rst