[Python-Dev] PEP 467: Minor API improvements to bytes, bytearray, and memoryview

Martin Panter vadmium+py at gmail.com
Tue Jun 7 22:01:14 EDT 2016

On 7 June 2016 at 20:28, Ethan Furman <ethan at stoneleaf.us> wrote:
> Addition of explicit "single byte" constructors
> -----------------------------------------------
> As binary counterparts to the text ``chr`` function, this PEP proposes the
> addition of an explicit ``byte`` alternative constructor as a class method
> on both ``bytes`` and ``bytearray``::
>     >>> bytes.byte(3)
>     b'\x03'
>     >>> bytearray.byte(3)
>     bytearray(b'\x03')

Bytes.byte() is a great idea. But what’s the point or use case of
bytearray.byte(), a mutable array of one pre-defined byte?

> Addition of optimised iterator methods that produce ``bytes`` objects
> ---------------------------------------------------------------------
> This PEP proposes that ``bytes``, ``bytearray`` and ``memoryview`` gain an
> optimised ``iterbytes`` method that produces length 1 ``bytes`` objects
> rather than integers::
>     for x in data.iterbytes():
>         # x is a length 1 ``bytes`` object, rather than an integer

Might be good to have an example with concrete output, so you see the
one-byte strings coming out of it.

>>> tuple(b"ABC".iterbytes())
(b'A', b'B', b'C')

More information about the Python-Dev mailing list