*Memos:
- My post explains the shallow and deep copy of a list.
- My post explains the shallow and deep copy of a tuple.
- My post explains the shallow copy of the set with a tuple.
- My post explains the shallow and deep copy of the set with an iterator.
- My post explains the shallow and deep copy of a dictionary.
- My post explains an iterator (1).
<Normal Copy>:
*Memos:
-
v1
andv2
refer to the same shallow and deep iterator. -
is
keyword can check ifv1
andv2
refer to the same iterator.
# Shallow iterator
# ↓↓↓↓↓↓ ↓↓
v1 = iter([iter(['a'])])
v2 = v1 # ↑↑↑↑↑↑↑↑↑↑↑
# Deep iterator
print(v1) # <list_iterator object at 0x0000029DDFFB7D30>
print(v2) # <list_iterator object at 0x0000029DDFFB7D30>
print(v1 is v2)
# True
print(next(v1)) # <list_iterator object at 0x0000029DDEC54E50>
print(next(v2)) # StopIteration:
<Shallow Copy>:
copy.copy() does shallow copy as shown below:
*Memos:
-
v1
andv2
refer to different shallow iterators. -
v1
andv2
refer to the same deep iterator.
import copy
v1 = iter([iter(['a'])])
v2 = copy.copy(v1)
print(v1) # <list_iterator object at 0x0000029DE015CDC0>
print(v2) # <list_iterator object at 0x0000029DE015F340>
print(v1 is v2)
# False
v3 = next(v1)
v4 = next(v2)
print(v3) # <list_iterator object at 0x0000029DD4BF82E0>
print(v4) # <list_iterator object at 0x0000029DD4BF82E0>
print(v3 is v4)
# True
print(next(v3)) # a
print(next(v4)) # StopIteration:
iter() doesn't do shallow copy as shown below:
v1 = iter([iter(['a'])])
v2 = iter(v1)
print(v1) # <list_iterator object at 0x00000192769592D0>
print(v2) # <list_iterator object at 0x00000192769592D0>
print(v1 is v2)
# True
print(next(v1)) # <list_iterator object at 0x000001927695B4F0>
print(next(v2)) # StopIteration:
<Deep Copy>:
deepcopy() does deep copy as shown below:
*Memos:
-
v1
andv2
refer to the different shallow and deep iterators. -
copy.deepcopy()
should be used because it's safe, doing copy deeply whilecopy.copy()
isn't safe, doing copy shallowly.
import copy
v1 = iter([iter(['a'])])
v2 = copy.deepcopy(v1)
print(v1) # <list_iterator object at 0x0000029DDF283E80>
print(v2) # <list_iterator object at 0x0000029DDF282080>
print(v1 is v2)
# False
v3 = next(v1)
v4 = next(v2)
print(v3) # <list_iterator object at 0x0000029DDF279D50>
print(v4) # <list_iterator object at 0x0000029DDF283FA0>
print(v3 is v4)
# False
print(next(v3)) # a
print(next(v4)) # a
The below with copy.copy() does shallow copy is equivalent to the above:
import copy
v1 = iter([iter(['a'])])
v2 = copy.copy(v1)
print(v1) # <list_iterator object at 0x0000029DDFF4D7E0>
print(v2) # <list_iterator object at 0x0000029DDFF4D2D0>
print(v1 is v2)
# False
v3 = copy(next(v1))
v4 = copy(next(v2))
print(v3) # <list_iterator object at 0x0000029DDFF4CEE0>
print(v4) # <list_iterator object at 0x0000029DDF283E80>
print(v3 is v4)
# False
print(next(v3)) # a
print(next(v4)) # a
Additionally, the below is a 3D iterator:
import copy
v1 = iter([iter([iter(['a'])])])
v2 = copy.deepcopy(v1)
print(v1) # <list_iterator object at 0x0000029DDF281EA0>
print(v2) # <list_iterator object at 0x0000029DDEAECB20>
print(v1 is v2)
# False
v3 = next(v1)
v4 = next(v2)
print(v3) # <list_iterator object at 0x0000029DDF282FE0>
print(v4) # <list_iterator object at 0x0000029DDFF4F760>
print(v3 is v4)
# False
v5 = next(v3)
v6 = next(v4)
print(v5) # <list_iterator object at 0x0000029DDEECEA40>
print(v6) # <list_iterator object at 0x0000029DDFF4FAC0>
print(v5 is v6)
# False
print(next(v5)) # a
print(next(v6)) # a