Python Glossary Русский

all Python 3until 3.9Removed from the standard libraryfunction

array.array.fromstring()

removed

Old name of array.frombytes(): appends items read from bytes to the array. Removed in Python 3.9; use frombytes() instead.

Syntax

a.fromstring(data)  # до 3.9

The method read bytes and appended them to the array as raw item values. The name came from Python 2, where bytes were called a string; Python 3.2 replaced it with frombytes(), and 3.9 removed the alias. How to rewrite: a.fromstring(data) becomes a.frombytes(data). If you used to pass a str, encode it first: a.frombytes(text.encode('ascii')). The byte count must be a multiple of a.itemsize.

Examples

# Python 3.9+
import array
a = array.array('B')
try:
    a.fromstring(b'hi')
except AttributeError as e:
    print(type(e).__name__)  # → AttributeError
# Python 3.9+
import array
a = array.array('B')
a.frombytes(b'hi')
print(a.tolist())  # → [104, 105]
a.frombytes('ok'.encode('ascii'))
print(a.tolist())  # → [104, 105, 111, 107]
import array
a = array.array('B')
a.fromstring(b'hi')
print(a.tolist())  # → [104, 105]

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy