[Python-Dev] Integer concatenation to byte string

45 views
Skip to first unread message

Memz

unread,
Mar 2, 2021, 4:43:05 PM3/2/21
to pytho...@python.org
Currently, the only way to concatenate an integer to a bytes object is by converting
the integer to bytes with a function call before concatenating. And there is no way to
make a mutable bytes object without a function call. And bytearray function calls are 9-10x slower than b-strings.

I propose an array-type string like the, or for the bytearray. It would work as a
mutable b-string, as

foo = a"\x00\x01\x02abcÿ"       # a-string, a mutable bytes object.
foo[0] = 123                              # Item assignment
foo+= 255                                 # Works the same as bytesvariable+=b"ÿ"
foo+= a"\x255\x00"                  # Concatenation with itself
foo+= b"\x255\x00"                  # Cross compatibility with bytes objects.

This would be processed the same as, or would be the bytearray,

>>> type(a"\x00\x01\x02abcÿ")
<class 'bytearray'>

Martin (gzlist) via Python-Dev

unread,
Mar 5, 2021, 10:39:45 AM3/5/21
to Memz, Python Dev
On Tue, 2 Mar 2021 at 21:44, Memz <mmax...@gmail.com> wrote:
>
> foo+= 255 # Works the same as bytesvariable+=b"ÿ"

foo = b"%b%d" % (foo, 255)

> foo+= a"\x255\x00" # Concatenation with itself

foo = b"%b%b" % (foo, foo)

See PEP461:

Adding % formatting to bytes and bytearray
<https://www.python.org/dev/peps/pep-0461/>
"With Python 3 and the split between str and bytes, one small but
important area of programming became slightly more difficult, and much
more painful -- wire format protocols."

Martin
_______________________________________________
Python-Dev mailing list -- pytho...@python.org
To unsubscribe send an email to python-d...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/pytho...@python.org/message/TM2XN4P6JMOFP3RUSFJLFPTDC5J77J7O/
Code of Conduct: http://python.org/psf/codeofconduct/
Reply all
Reply to author
Forward
0 new messages