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

Paul Sokolovsky pmiscml at gmail.com
Tue Jun 7 17:33:50 EDT 2016


Hello,

On Tue, 07 Jun 2016 13:28:13 -0700 Ethan Furman <ethan at stoneleaf.us> wrote:

Minor changes: updated version numbers, add punctuation.

The current text seems to take into account Guido's last comments. Thoughts before asking for acceptance? []

Deprecation of current "zero-initialised sequence" behaviour ------------------------------------------------------------

Currently, the bytes and bytearray constructors accept an integer argument and interpret it as meaning to create a zero-initialised sequence of the given size:: >>> bytes(3) b'\x00\x00\x00' >>> bytearray(3) bytearray(b'\x00\x00\x00') This PEP proposes to deprecate that behaviour in Python 3.6, and remove it entirely in Python 3.7.

Why the desire to break applications of thousands and thousands of people? Besides, bytes(3) behavior is very logical. Everyone who knows what malloc(3) does also knows what bytes(3) does. Who doesn't, can learn, and eventually be grateful that learning Python actually helped them to learn other language as well.

[]

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') These methods will only accept integers in the range 0 to 255 (inclusive):: >>> bytes.byte(512) Traceback (most recent call last): File "", line 1, in ValueError: bytes must be in range(0, 256) >>> bytes.byte(1.0) Traceback (most recent call last): File "", line 1, in TypeError: 'float' object cannot be interpreted as an integer The documentation of the ord builtin will be updated to explicitly note that bytes.byte is the inverse operation for binary data, while chr is the inverse operation for text data.

The documentation should probably also mention that bytes.byte(x) is equivalent to x.to_bytes(1, "little").

[]

-- Best regards, Paul mailto:pmiscml at gmail.com



More information about the Python-Dev mailing list