Below is a sample interaction in Python 3.3
>>> bArray = bytes(b'ABCDE')
>>> bArray
b'ABCDE'
>>> bArray[0]
65
>>> type(bArray[0])
<class 'int'>
>>> bArray[0:1]
b'A'
>>> type(bArray[0:1])
<class 'bytes'>
>>> struct.pack('B', bArray[1])
b'B'
>>> type(struct.pack('B', bArray[1]))
<class 'bytes'>
It shows that indexing a bytes array yields an integer while slicing the same returns a bytes object.
- Can anyone please explain me why is it so? Shouldn't indexing, too, return a byte object?
- Is using byte more memory/performance efficient that using plain integers, provided the application is guaranteed to use numbers [0,255]?