8000 [3.12] gh-114115: Update documentation of array.array (GH-114117) (GH… · python/cpython@7da1750 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7da1750

Browse files
[3.12] gh-114115: Update documentation of array.array (GH-114117) (GH-114417)
(cherry picked from commit 650f9e4) Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent 33c1907 commit 7da1750

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

Doc/library/array.rst

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,16 @@ The module defines the following type:
7676
.. class:: array(typecode[, initializer])
7777

7878
A new array whose items are restricted by *typecode*, and initialized
79-
from the optional *initializer* value, which must be a list, a
80-
:term:`bytes-like object`, or iterable over elements of the
81-
appropriate type.
79+
from the optional *initializer* value, which must be a :class:`bytes`
80+
or :class:`bytearray` object, a Unicode string, or iterable over elements
81+
of the appropriate type.
8282

83-
If given a list or string, the initializer is passed to the new array's
84-
:meth:`fromlist`, :meth:`frombytes`, or :meth:`fromunicode` method (see below)
85-
to add initial items to the array. Otherwise, the iterable initializer is
86-
passed to the :meth:`extend` method.
83+
If given a :class:`bytes` or :class:`bytearray` object, the initializer
84+
is passed to the new array's :meth:`frombytes` method;
85+
if given a Unicode string, the initializer is passed to the
86+
:meth:`fromunicode` method;
87+
otherwise, the initializer's iterator is passed to the :meth:`extend` method
88+
to add initial items to the array.
8789

8890
Array objects support the ordinary sequence operations of indexing, slicing,
8991
concatenation, and multiplication. When using slice assignment, the assigned
@@ -149,10 +151,11 @@ The module defines the following type:
149151
must be the right type to be appended to the array.
150152

151153

152-
.. method:: frombytes(s)
154+
.. method:: frombytes(buffer)
153155

154-
Appends items from the string, interpreting the string as an array of machine
155-
values (as if it had been read from a file using the :meth:`fromfile` method).
156+
Appends items from the :term:`bytes-like object`, interpreting
157+
its content as an array of machine values (as if it had been read
158+
from a file using the :meth:`fromfile` method).
156159

157160
.. versionadded:: 3.2
158161
:meth:`!fromstring` is renamed to :meth:`frombytes` for clarity.
@@ -174,9 +177,9 @@ The module defines the following type:
174177

175178
.. method:: fromunicode(s)
176179

177-
Extends this array with data from the given unicode string. The array must
178-
be a type ``'u'`` array; otherwise a :exc:`ValueError` is raised. Use
179-
``array.frombytes(unicodestring.encode(enc))`` to append Unicode data to an
180+
Extends this array with data from the given Unicode string.
181+
The array must have type code ``'u'``; otherwise a :exc:`ValueError` is raised.
182+
Use ``array.frombytes(unicodestring.encode(enc))`` to append Unicode data to an
180183
array of some other type.
181184

182185

@@ -236,23 +239,27 @@ The module defines the following type:
236239

237240
.. method:: tounicode()
238241

239-
Convert the array to a unicode string. The array must be a type ``'u'`` array;
242+
Convert the array to a Unicode string. The array must have a type ``'u'``;
240243
otherwise a :exc:`ValueError` is raised. Use ``array.tobytes().decode(enc)`` to
241-
obtain a unicode string from an array of some other type.
244+
obtain a Unicode string from an array of some other type.
242245

243246

244-
When an array object is printed or converted to a string, it is represented as
245-
``array(typecode, initializer)``. The *initializer* is omitted if the array is
246-
empty, otherwise it is a string if the *typecode* is ``'u'``, otherwise it is a
247-
list of numbers. The string is guaranteed to be able to be converted back to an
247+
The string representation of array objects has the form
248+
``array(typecode, initializer)``.
249+
The *initializer* is omitted if the array is empty, otherwise it is
250+
a Unicode string if the *typecode* is ``'u'``, otherwise it is
251+
a list of numbers.
252+
The string representation is guaranteed to be able to be converted back to an
248253
array with the same type and value using :func:`eval`, so long as the
249254
:class:`~array.array` class has been imported using ``from array import array``.
255+
Variables ``inf`` and ``nan`` must also be defined if it contains
256+
corresponding floating point values.
250257
Examples::
251258

252259
array('l')
253260
array('u', 'hello \u2641')
254261
array('l', [1, 2, 3, 4, 5])
255-
array('d', [1.0, 2.0, 3.14])
262+
array('d', [1.0, 2.0, 3.14, -inf, nan])
256263

257264

258265
.. seealso::

0 commit comments

Comments
 (0)
0