I was checking the documentation [1] to see where to put the new information 
about bytes and bytearray %-formatting, and
noticed that /every/ operation that could modify a bytearray object in place 
(e.g. center, capitalize, strip, etc.)
instead returns a new copy.

The section just prior to that [2] does say, "As bytearray objects are mutable, 
they support the mutable sequence
operations ...".

So it seems that when bytearray is treated as ascii data a new bytearry is 
returned, and when bytearray is treated as a
container it is modified in place:

New:

  bytearray(b'Chapter Title').center()
  bytearray(b' Chapter Title ').replace(b' ', b'- * - ')

In-place:

  bytearray(b'abc'][1] = ord(b'z')
  bytearray(b'def'] += b'ghi'
  bytearray(b'123'] *= 3

I now have a minor dilemma:  %-formatting is an ascii operation, but it also 
has an in-place operator (%=) . . . so does
%= modify the bytearray in place just like += and *= do, or does it return a 
new bytearray just like all the named ascii
operations do?  I do not know which is more surprising: having one of the 
in-place operators not work in place, or
having an unnamed ascii-operation not return a new copy.

Thoughts?

--
~Ethan~


[1] 
https://docs.python.org/3/library/stdtypes.html#bytes-and-bytearray-operations
[2] https://docs.python.org/3/library/stdtypes.html#bytearray-objects

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to