Integer concatenation to byte string

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. 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"123" 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'>

mmax42852@gmail.com writes:
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.
I don't work with bytes much, so I won't comment on the proposal itself. But as somebody who works a lot in email, where ASCII vs. everything else is a really important distinction, that looks like 'ASCII string "\x00\x01\x02abcÿ"' to me (despite the binary notations). So I'd prefer a different prefix. Steve

On 1 Mar 2021, at 18:01, mmax42852@gmail.com wrote:
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.
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"123" 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'>
When I needed to do the creation of bytes objects from a mix of types the struct.pack() method has been the obvious way to go. What is the use case that leads to needing the above? Barry
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/H5W3TT... Code of Conduct: http://python.org/psf/codeofconduct/

On Mon, Mar 01, 2021 at 06:01:24PM -0000, mmax42852@gmail.com wrote:
I propose an array-type string like the, or for the bytearray. It would work as a mutable b-string, as [...] This would be processed the same as, or would be the bytearray,
Then just use bytearray. I think the only new part is to provide a literal syntax for bytearray: a = a"xyz" # same as bytearray(b"xyz") But we already have a bunch of prefixes: b B u U r R f F plus various combinations and this would presumably add six more: a A ar aR Ar AR Literal syntax is a convenience, its not essential, so I guess this proposal requires justification for why bytearray is important enough to justify literal syntax. -- Steve

On Tue, Mar 2, 2021 at 5:03 AM <mmax42852@gmail.com> wrote:
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.
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"123" 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'>
A bytearray already has an append method that does what you want.
foo = bytearray(b"\x00\x01\x02abc") foo.append(255) foo bytearray(b'\x00\x01\x02abc\xff')
Adding a string and a number is, in most languages where it's legal, interpreted as "represent this number in digits, then append those digits to the string". Having it mean the opposite here would be very confusing. So, you're asking for a couple of things here. One, an easier way to append one byte to a bytearray. Two, a literal syntax for bytearrays. I'd recommend splitting this into those two, or focusing on the one that's more important to you; at the moment, you haven't really made much of a case for either. (Note that your a-string, if it is indeed simply a bytearray, is already able to have other bytearrays and bytes strings concatenated onto it.) So go for it: convince us! :) Tell us why a literal syntax (or technically, a "display" syntax) for bytearrays is important; and/or tell us why it's important to have an operator for adding an integer onto a bytearray. ChrisA

On 3/1/21, mmax42852@gmail.com <mmax42852@gmail.com> wrote:
And there is no way to make a mutable bytes object without a function call.
Since a code object is immutable, the proposed bytearray display form would still require an internal operation that constructs a bytearray from a bytes object. For example, something like the following: BUILD_BYTEARRAY 0 LOAD_CONST 0 (b'spam') BYTEARRAY_EXTEND 1
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
Concatenating a sequence with a number shouldn't be allowed. OTOH, I think `foo += [255]` should be supported as foo.extend([255]), but bytearray doesn't allow it currently. `foo.append(255)` is supported.
participants (7)
-
Barry Scott
-
Chris Angelico
-
Eryk Sun
-
Greg Ewing
-
mmax42852@gmail.com
-
Stephen J. Turnbull
-
Steven D'Aprano