A bytes
object yield int
values when being iterated over:
>>> list(b'abc')
[97, 98, 99]
I find myself wanting something like this:
>>> b'abc'.to_list_of_single_bytes()
[b'a', b'b', b'c']
I thought that .split()
is the method I was looking for, but apparently it rejects empty separators, unlike, say, JavaScript’s .split()
:
>>> b'abc'.split(b'')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: empty separator
Of course, the result can also be achieved by a number of other ways:
>>> [bytes([byte]) for byte in b'abc']
[b'a', b'b', b'c']
>>>
>>> [byte.to_bytes(1, 'big') for byte in b'abc']
[b'a', b'b', b'c']
>>>
>>> import re
>>> re.findall(b'.', b'abc', flags = re.S)
[b'a', b'b', b'c']
…but all of them are somewhat verbose and do not look quite as “nice” as list(b'abc')
.
Is there a better way that I’m ignorant of? If not, would it be suitable to add such a method to the language?