From 4c49c882feb5860b87599dce44a7f69ddd4d6c29 Mon Sep 17 00:00:00 2001 From: Umar Abdulaziz <129359580+synckerz@users.noreply.github.com> Date: Sun, 8 Nov 2026 17:06:19 +0300 Subject: [PATCH 1/3] Add example of mutable list sharing Document how augmented assignment can modify a shared list and demonstrate using copy() to avoid unintended side effects. --- docs/writing/structure.rst | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/docs/writing/structure.rst b/docs/writing/structure.rst index 5d9645acd..0225a40d5 100644 --- a/docs/writing/structure.rst +++ b/docs/writing/structure.rst @@ -780,6 +780,40 @@ mutables are lists and dictionaries: All lists have mutating methods, like :py:meth:`list.append` or :py:meth:`list.pop`, and can be modified in place. The same goes for dictionaries. + +.. note:: + + Be careful when assigning mutable objects to new variables. + Assigning a list to another variable does not create a copy; + both variables refer to the same list. Modifying the list + through either variable will affect the shared object. + + For example: + + .. code-block:: python + + class Data: + mylist = [1, 2, 3, 4] + + def test_function(): + local_list = Data.mylist + local_list += [5] + + test_function() + print(Data.mylist) # [1, 2, 3, 4, 5] + + To avoid modifying the original list, create a copy before + making changes: + + .. code-block:: python + + def test_function(): + local_list = Data.mylist.copy() + local_list += [5] + test_function() + print(Data.mylist) # [1, 2, 3, 4] + + Immutable types provide no method for changing their content. For instance, the variable x set to the integer 6 has no "increment" method. If you want to compute x + 1, you have to create another integer and give it a name. From 8af9d577e46e3857d0f931b85ee378af8b7a06dd Mon Sep 17 00:00:00 2001 From: Umar Abdulaziz <129359580+synckerz@users.noreply.github.com> Date: Tue, 10 Nov 2026 02:26:11 +0300 Subject: [PATCH 2/3] Fix indentation in mutable list example --- docs/writing/structure.rst | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/docs/writing/structure.rst b/docs/writing/structure.rst index 0225a40d5..f83f7d0ff 100644 --- a/docs/writing/structure.rst +++ b/docs/writing/structure.rst @@ -788,7 +788,7 @@ The same goes for dictionaries. both variables refer to the same list. Modifying the list through either variable will affect the shared object. - For example: +For example: .. code-block:: python @@ -810,22 +810,9 @@ The same goes for dictionaries. def test_function(): local_list = Data.mylist.copy() local_list += [5] - test_function() - print(Data.mylist) # [1, 2, 3, 4] - -Immutable types provide no method for changing their content. For instance, the -variable x set to the integer 6 has no "increment" method. If you want to -compute x + 1, you have to create another integer and give it a name. - -.. code-block:: python - - my_list = [1, 2, 3] - my_list[0] = 4 - print(my_list) # [4, 2, 3] <- The same list has changed - - x = 6 - x = x + 1 # The new x is another object + test_function() + print(Data.mylist) # [1, 2, 3, 4] One consequence of this difference in behavior is that mutable types are not "stable", and therefore cannot be used as dictionary From 11b58f0cc7266989740df5cae4b9ae015fa31fb1 Mon Sep 17 00:00:00 2001 From: Umar Abdulaziz <129359580+synckerz@users.noreply.github.com> Date: Tue, 10 Nov 2026 02:30:45 +0300 Subject: [PATCH 3/3] Update examples in structure.rst for clarity Clarified examples of mutable and immutable types in Python. --- docs/writing/structure.rst | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/docs/writing/structure.rst b/docs/writing/structure.rst index f83f7d0ff..98e7acf60 100644 --- a/docs/writing/structure.rst +++ b/docs/writing/structure.rst @@ -788,7 +788,7 @@ The same goes for dictionaries. both variables refer to the same list. Modifying the list through either variable will affect the shared object. -For example: + For example: .. code-block:: python @@ -814,6 +814,19 @@ For example: test_function() print(Data.mylist) # [1, 2, 3, 4] + +Immutable types provide no method for changing their content. For instance, the +variable x set to the integer 6 has no "increment" method. If you want to +compute x + 1, you have to create another integer and give it a name. + +.. code-block:: python + + my_list = [1, 2, 3] + my_list[0] = 4 + print(my_list) # [4, 2, 3] <- The same list has changed + + x = 6 + x = x + 1 # The new x is another object One consequence of this difference in behavior is that mutable types are not "stable", and therefore cannot be used as dictionary keys.