Python Glossary Русский

Python 3.3+Byte sequencesfunction

bytearray.copy

bytearray — mutating

Return a shallow copy (a new bytearray).

Syntax

ba.copy(...)

"Shallow" costs you nothing here: the elements are plain ints, so the copy is fully independent, unlike a list holding nested lists. bytearray(ba) and ba[:] give the same result, while bytes(ba) yields an immutable snapshot of the same content.

Examples

print(bytearray(b'abc').copy())   # → bytearray(b'abc')
ba = bytearray(b'abc'); c = ba.copy(); c.append(100); print(ba, c)   # → bytearray(b'abc') bytearray(b'abcd')
ba = bytearray(b'abc'); alias = ba; alias.append(122); print(ba)   # → bytearray(b'abcz')
ba = bytearray(b'abc'); print(ba.copy() == ba, ba.copy() is ba)   # → True False
print(bytearray().copy())   # → bytearray(b'')

See also

Open in the full glossarydocs.python.orgReport an inaccuracy