On Tue, Mar 2, 2021 at 5:03 AM <mmax42...@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
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@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/JDMVY2QNGMYXG56576TGZYNIMQ4LAMIG/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to