*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 bytes.
- My post explains a bytes.
Different bytearrays are referred to if copied.
A 2D bytearray is experimented, doing assignment and shallow and deep copy as shown below:
*Memo:
- For a 2D bytearray, only shallow copy is possible.
- A bytearray has 2 demesions(D).
<Assignment>:
*Memo:
-
v1
andv2
refer to the same shallow and deep bytearray. -
is
keyword can check ifv1
andv2
refer to the same bytearray.
A 2D bytearray is assigned to a variable without copied as shown below:
# Shallow bytearray
# ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
v1 = bytearray(b'abcde')
v2 = v1 # ↑↑↑↑↑ Deep bytearray(Each byte)
print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'abcde')
print(v1 is v2, v1[2] is v2[2])
# True True True True
v2[1] = ord('X')
v2[3] = ord('Y')
print(v1) # bytearray(b'aXcYe')
print(v2) # bytearray(b'aXcYe')
<Shallow copy>:
*Memo:
-
v1
andv2
refer to different shallow bytearrays. -
v1
andv2
refer to the same deep bytearray.
bytearray.copy() shallow-copies the 2D bytearray as shown below:
v1 = bytearray(b'abcde')
v2 = v1.copy()
print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'abcde')
print(v1 is v2, v1[2] is v2[2])
# False True
v2[1]= ord('X')
v2[3]= ord('Y')
# ↓ ↓
print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'aXcYe')
# ↑ ↑
copy.copy() shallow-copies a 2D bytearray as shown below:
import copy
v1 = bytearray(b'abcde')
v2 = copy.copy(v1)
print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'abcde')
print(v1 is v2, v1[2] is v2[2])
# False True
v2[1]= ord('X')
v2[3]= ord('Y')
# ↓ ↓
print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'aXcYe')
# ↑ ↑
Slicing shallow-copies the 2D bytearray as shown below:
v1 = bytearray(b'abcde')
v2 = v1[:]
print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'abcde')
print(v1 is v2, v1[2] is v2[2])
# False True
v2[1]= ord('X')
v2[3]= ord('Y')
# ↓ ↓
print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'aXcYe')
# ↑ ↑
bytearray() shallow-copies a 2D bytearray as shown below:
v1 = bytearray(b'abcde')
v2 = bytearray(v1)
print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'abcde')
print(v1 is v2, v1[2] is v2[2])
# False True
v2[1]= ord('X')
v2[3]= ord('Y')
# ↓ ↓
print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'aXcYe')
# ↑ ↑
<Deep copy>:
*Memo:
-
v1
andv2
refer to different shallow bytearrays. -
v1
andv2
refer to the same deep bytearray.
copy.deepcopy() doesn't deep-copy a 2D bytearray as shown below:
import copy
v1 = bytearray(b'abcde')
v2 = copy.deepcopy(v1)
print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'abcde')
print(v1 is v2, v1[2] is v2[2])
# False True
v2[1]= ord('X')
v2[3]= ord('Y')
# ↓ ↓
print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'aXcYe')
# ↑ ↑
Top comments (0)