12.07.18 08:33, Gregory P. Smith пише:
Agreed, bytearray(b'...') should be way less common. I don't
immediately have a use for that beyond merely than disliking the copy
from temporary bytes object and gc behind the scenes.
You can't avoid this since bytearray is mutable. The constant bytes
argument can be shared, but the content of a new bytearray needs to be
copied.
a = b'abc'
b = bytearray(b'abc') # should make a copy
c = bytearray(b'abc') # should make a copy
b[0] = 0
assert c[0] == 97
Although there is a possibility to apply in bytearray the same
optimization as was made in BytesIO. The bytearray object can use an
internal mutable bytes object for storing a content instead of a raw
array. The constructor can save a reference to the passed bytes object,
this is O(1) operation. bytes(bytearray) could just return a reference
to that bytes object, it is O(1) too. Any mutating operation should
check the refcount and make a copy if it is not 1. This will complicate
the code, and I'm not sure if it is worth.
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/