[Python-Dev] Adding bytes.frombuffer() constructor to PEP 467 (was: [Python-ideas] Adding bytes.frombuffer() constructor

INADA Naoki songofacandy at gmail.com
Wed Oct 12 22:59:00 EDT 2016


>
> Also, why do the conversion from bytearray to bytes?  It is definitely not
> always needed.
>
>>>> ba = bytearray(b'abc')
>>>> b = b'def'
>>>> ba + b
> bytearray(b'abcdef')
>>>> b'%s %s' % (ba, b)
> b'abc def'
>>>> b + ba
> b'defabc'
>>>> ba.extend(b)
>>>> ba
> bytearray(b'abcdef')
>
> Even if it is sometimes needed, why do it always?  The essence of read_line
> is to slice out a line, delete it from the buffer, and return the line.  Let
> the caller explicitly convert when needed.
>
> --
> Terry Jan Reedy
>

Because it's public module API.

While bytearray is mostly API compatible (passes duck typing),
isinstance(b, bytes) is False when b is bytearray.

So, I feel changing return type from bytes to bytearray is last option.
I want to return bytes if possible.

-- 
INADA Naoki  <songofacandy at gmail.com>


More information about the Python-Dev mailing list