*Memos:
- My post explains a byte string.
-
My post explains a byte string(
bytes
) with indexing and slicing. - My post explains bytes().
- My post explains bytearray().
- My post explains a string.
The byte string of bytearray()
can be read by indexing or slicing as shown below:
*Memos:
- Indexing can be done with one or more
[index]
. - Slicing can be done with one or more
[start:end:step]
:-
start
(Optional-Default:The index of the 1st element
). -
end
(Optional-Default:The index of the last element + 1
). -
step
(Optional-Default:1
). *step
cannot be zero. - The
[]
with at least one:
is slicing.
-
v = bytearray(b'ABCDEFGH')
print(v)
# bytearray(b'ABCDEFGH')
print(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7])
print(v[-8], v[-7], v[-6], v[-5], v[-4], v[-3], v[-2], v[-1])
# 65 66 67 68 69 70 71 72
v = bytearray(b'ABCDEFGH')
print(v[:])
print(v[::])
# bytearray(b'ABCDEFGH')
print(v[::2])
# bytearray(b'ACEG')
print(v[::-2])
# bytearray(b'HFDB')
print(v[2:])
print(v[-6:])
print(v[2::])
print(v[-6::])
# bytearray(b'CDEFGH')
print(v[2::2])
print(v[-6::2])
# bytearray(b'CEG')
print(v[2::-2])
print(v[-6::-2])
# bytearray(b'CA')
print(v[:6])
print(v[:-2])
print(v[:6:])
print(v[:-2:])
# bytearray(b'ABCDEF')
print(v[:6:2])
print(v[:-2:2])
# bytearray(b'ACE')
print(v[:6:-2])
print(v[:-2:-2])
# bytearray(b'H')
print(v[2:6])
print(v[-6:-2])
print(v[2:6:])
print(v[-6:-2:])
# bytearray(b'CDEF')
print(v[2:6:2])
print(v[-6:-2:2])
# bytearray(b'CE')
print(v[2:6:-2])
print(v[-6:-2:-2])
# bytearray(b'')
The byte string of bytearray()
can be changed by indexing or slicing as shown below:
*Memos:
- An iterable must be assigned to a sliced variable.
- A del statement can be used to remove one or more bytes from a list by indexing or slicing and can remove one or more variables themselves.
v = bytearray(b'abcdef')
v[0] = ord(b'X')
v[2:6] = [ord(b'Y'), ord(b'Z')]
print(v)
# bytearray(b'XbYZ')
print(bytes(v))
# b'XbYZ'
v = bytearray(b'abcdef')
del v[0], v[3:5]
print(v)
# bytearray(b'bcd')
print(bytes(v))
# b'bcd'
v = bytearray(b'abcdef')
del v
print(v)
# NameError: name 'v' is not defined
The variables v1
and v2
refer to the same byte string of bytearray()
unless copied as shown below:
*Memos:
-
is
keyword can check ifv1
andv2
refer to the same byte string. -
bytearray().copy()
, copy.copy() and slicing do shallow copy. *bytearray().copy()
has no arguments. - copy.deepcopy() does deep copy.
-
copy.deepcopy()
should be used because it's safe, doing copy deeply whilebytearray().copy()
,copy.copy()
and slicing aren't safe, doing copy shallowly.
import copy
v1 = bytearray(b'abcde')
v2 = v1 # v2 refers to the same byte string as v1.
v2[2] = ord('X') # Changes the same byte string as v1.
v2[2] = 88
# ↓
print(v1) # bytearray(b'abXde')
print(v2) # bytearray(b'abXde')
# ↑
print(v1 is v2) # True
v2 = v1.copy() # v2 refers to the different byte string from v1.
v2 = copy.copy(v1)
v2 = copy.deepcopy(v1)
v2 = v1[:]
v2[2] = ord('Y') # Changes the different byte string from v1.
v2[2] = 89
# ↓
print(v1) # bytearray(b'abXde')
print(v2) # bytearray(b'abYde')
# ↑
print(v1 is v2) # False