Python Glossary Русский

all Python 3until 3.9Removed from the standard libraryfunction

array.array.tostring()

removed

Old name of array.tobytes(): the array contents as bytes. Removed in Python 3.9; use tobytes() instead.

Syntax

a.tostring()  # до 3.9

In Python 2 a "string" meant bytes, hence the name tostring(). Python 3.2 renamed the method to tobytes() to avoid confusion with str, kept the old name as an alias and dropped it in 3.9. How to rewrite: replace a.tostring() with a.tobytes(); the result is the same bytes holding the machine representation of the items. If you need text, decode the bytes explicitly: a.tobytes().decode('ascii').

Examples

# Python 3.9+
import array
a = array.array('B', [104, 105])
try:
    a.tostring()
except AttributeError as e:
    print(type(e).__name__)  # → AttributeError
# Python 3.9+
import array
a = array.array('B', [104, 105])
print(a.tobytes())  # → b'hi'
import array
a = array.array('B', [104, 105])
print(a.tostring())  # → b'hi'

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy