[Python-ideas] Adding bytes.frombuffer() constructor

INADA Naoki songofacandy at gmail.com
Sat Aug 6 20:44:58 EDT 2016


bytearray is used as read / write buffer in asyncio.

When implementing reading stream, read() API may be defined
as returns "bytes".
To get front n bytes from bytearray, there are two ways.

1. bytes(bytearray[:n])
2. bytes(memoryview(bytearray)[:n])

(1) is simplest, but it produces temporary bytearray having n bytes.

While (2) is more efficient than (1), it uses still temporary memoryview
object, and it looks bit tricky.

I want simple, readable, and temporary-less way to get part of bytearray
as bytes.

The API I propose looks like this:

bytes.frombuffer(byteslike, length=-1, offset=0)

How do you feel about it?

-- 
INADA Naoki  <songofacandy at gmail.com>


More information about the Python-ideas mailing list