PEP 467: add `.ascii` alternative constructor

This commit is contained in:
Ethan Furman 2021-11-02 21:37:35 -07:00
parent 2b41b5229a
commit 8681c6b7d9
1 changed files with 25 additions and 5 deletions

View File

@ -17,11 +17,11 @@ Abstract
This PEP proposes five small adjustments to the APIs of the ``bytes`` and
``bytearray`` types to make it easier to operate entirely in the binary domain:
* Add ``bytes.fromsize`` and ``bytearray.fromsize`` alternative constructors
* Add ``bytes.fromint`` and ``bytearray.fromint`` alternative constructors
* Add ``bytes.getbyte`` and ``bytearray.getbyte`` byte retrieval methods
* Add ``bytes.iterbytes`` and ``bytearray.iterbytes`` alternative iterators
* Add ``fromsize`` alternative constructor
* Add ``fromint`` alternative constructor
* Add ``ascii`` alternative constructor
* Add ``getbyte`` byte retrieval method
* Add ``iterbytes`` alternative iterator
Rationale
=========
@ -119,6 +119,26 @@ negative numbers. The documentation of the new methods will refer readers to
``int.to_bytes`` for use cases where handling of arbitrary integers is needed.
Addition of "ascii" constructors
--------------------------------
In Python 2 converting an object, such as the integer ``123``, to bytes (aka the
Python 2 ``str``) was as simple as::
>>> str(123)
'123'
With Python 3 that became the more verbose::
>>> str(123).encode('ascii')
This PEP proposes that an ``ascii`` method be added to ``bytes`` and ``bytearray``
to handle this use-case::
>>> bytes.ascii(123)
b'123'
Addition of "getbyte" method to retrieve a single byte
------------------------------------------------------