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

Michael Selik michael.selik at gmail.com
Sun Aug 7 01:08:26 EDT 2016


On Sat, Aug 6, 2016 at 8:45 PM INADA Naoki <songofacandy at gmail.com> wrote:

> 1. bytes(bytearray[:n])
> 2. bytes(memoryview(bytearray)[:n])
>
> (1) is simplest, but it produces temporary bytearray having n bytes.
>

Does that actually make the difference between unacceptably inefficient
performance and acceptably efficient for an application you're working on?


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

Using the memoryview is nicely explicit whereas ``bytes.frombuffer`` could
be creating a temporary bytearray as part of its construction.

The API I propose looks like this:
> bytes.frombuffer(byteslike, length=-1, offset=0)
>

RawIOBase.read and the other read methods described in the io module use
the parameter "size" instead of "length".
https://docs.python.org/3/library/io.html#io.RawIOBase
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160807/a7f8ad6a/attachment.html>


More information about the Python-ideas mailing list