*Memo:
- 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 and deep copy of the set with a frozenset.
- My post explains the shallow and deep 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 frozenset.
- My post explains the shallow and deep copy of a dictionary.
- My post explains the shallow and deep copy of an iterator.
- My post explains the shallow and deep copy of an string.
- My post explains the shallow and deep copy of a bytearray.
- My post explains a bytes.
The same bytes is referred to even if copied.
A 2D bytes is experimented, doing assignment and shallow and deep copy as shown below:
*Memo:
- For a 2D bytes, both shallow and deep copy are impossible.
- A bytes has 2 demesions(D).
<Assignment>:
*Memo:
-
v1
andv2
refer to the same shallow and deep bytes. -
is
keyword can check ifv1
andv2
refer to the same bytes.
A 2D bytes is assigned to a variable without copied as shown below:
# Shallow bytes
# ↓↓↓↓↓↓↓↓
v1 = b'abcde'
# ↑↑↑↑↑ Deep bytes(Each byte)
v2 = v1
print(v1, v1[2]) # b'abcde' 99
print(v2, v2[2]) # b'abcde' 99
print(v1 is v2, v1[2] is v2[2])
# True True
<Shallow copy>:
*Memo:
-
v1
andv2
refer to the same shallow and deep bytes.
copy.copy() doesn't shallow-copy a 2D bytes as shown below:
import copy
v1 = b'abcde'
v2 = copy.copy(v1)
print(v1, v1[2]) # abcde c
print(v2, v2[2]) # abcde c
print(v1 is v2, v1[2] is v2[2])
# True True
Slicing doesn't shallow-copy the 2D bytes as shown below:
v1 = b'abcde'
v2 = v1[:]
print(v1, v1[2]) # b'abcde' 99
print(v2, v2[2]) # b'abcde' 99
print(v1 is v2, v1[2] is v2[2])
# True True
str() doesn't shallow-copy a 2D bytes as shown below:
v1 = b'abcde'
v2 = bytes(v1)
print(v1, v1[2]) # b'abcde' 99
print(v2, v2[2]) # b'abcde' 99
print(v1 is v2, v1[2] is v2[2])
# True True
<Deep copy>:
*Memo:
-
v1
andv2
refer to the same shallow and deep bytes.
copy.deepcopy() doesn't deep-copy a 2D bytes as shown below:
import copy
v1 = b'abcde'
v2 = copy.deepcopy(v1)
print(v1, v1[2]) # b'abcde' 99
print(v2, v2[2]) # b'abcde' 99
print(v1 is v2, v1[2] is v2[2])
# True True
Top comments (0)