This article would be updating for long long term, which is used to record every details of my programming career. Maybe you also often make a wrong programe which is non-compliant. I hope this article could help you and me to develop good programming habits, so that we can program rapidly, reasonably and correctly.
- list direct assignment, list.copy() and
- improper global variables use and forget reseting the global flag after finished
list direct assignment, list.copy() and
what is the differences of tmp = list_a
, tmp = list_a.copy()
and tmp = copy.deepcopy(list_a)
a reference to the original list with always the same changes
tmp = list_a (Assignment):
- This creates a reference to the original list, list_a.
- Any changes made to tmp will directly affect list_a because they both point to the same memory location.
- This is known as a shallow reference.
Shallow Copy
- This creates a new list with a shallow copy of list_a’s elements.
- The new list, tmp, is separate from list_a, so modifying tmp won’t affect list_a (and vice versa) if they contain only simple data types (like integers or strings).
- However, if list_a contains mutable elements (like other lists or dictionaries), tmp will still reference those same nested objects. Therefore, modifying these nested elements in tmp will also affect list_a.
Take notice, mutable elements are the same such as a sublist as its element: [5,6,7] and [9,10]. The object in the list would have the same changes if you changed the new copy or original list.
But the single simple element are seperately, you change 1 to 2, they won’t impact mutually.
# [5,6,7] and [9,10] are mutable elements
a = [1,2,3,4, [5,6,7], 8, [9,10]]
b = a.copy()
print("a: ", a)
print("b: ", b)
# won't change the original
b[0] = 11
print("a: ", a)
print("b: ", b)
# Will change the original
b[4][0] = 12
print("a: ", a)
print("b: ", b)
a: [1, 2, 3, 4, [5, 6, 7], 8, [9, 10]]
b: [1, 2, 3, 4, [5, 6, 7], 8, [9, 10]]
a: [1, 2, 3, 4, [5, 6, 7], 8, [9, 10]]
b: [11, 2, 3, 4, [5, 6, 7], 8, [9, 10]]
a: [1, 2, 3, 4, [12, 6, 7], 8, [9, 10]]
b: [11, 2, 3, 4, [12, 6, 7], 8, [9, 10]]
deep copy
tmp = copy.deepcopy(list_a)
(Deep Copy):
This creates a completely independent copy of list_a, including all nested objects.
Any changes made to tmp or its nested elements won’t affect list_a, and vice versa.
This approach is useful when working with complex, nested structures where you want a fully independent duplicate of the original list.
conclusion
- Assignment (tmp = list_a): tmp and list_a are the same object.
- Shallow Copy (tmp = list_a.copy()): tmp is a new list, but it shares nested objects with list_a.
- Deep Copy (tmp = copy.deepcopy(list_a)): tmp is a completely independent copy, including all nested objects.
improper global variables use and forget reseting the global flag after finished
During 20th, 21st Oct 2024, I was doing Martin’s large assignment 1. In the solitaire case, I wrongly used the ROUND_FLAG=1
to be a flag to record the rounds of the game. Hence, when I singly run the program, then it would generate correct result; but if I called simulate() for many times, it would generate wrong results. For example, I put 7 as the random seed, then it would generate correct result; when I give 7 also to simulate(), I debugged and saw 7 into the seed variable, but it would generate wrong result.
Then I found the global variable mattered. T_T . I shouldn’t use global variable, because it’s not worthy to use global variable, and then, I should reset it after every round.
It is a good thought that reset every global variable after running the functions, if you want to call your function with the global variables, in one script to call for many times at once.
Welcome to point out the mistakes and faults!