@@ -79,14 +79,16 @@ The module defines the following type:
79
79
.. class :: array(typecode[, initializer])
80
80
81
81
A new array whose items are restricted by *typecode *, and initialized
82
- from the optional *initializer * value, which must be a list, a
83
- :term: ` bytes-like object`, or iterable over elements of the
84
- appropriate type.
82
+ from the optional *initializer * value, which must be a :class: ` bytes `
83
+ or :class: ` bytearray ` object, a Unicode string, or iterable over elements
84
+ of the appropriate type.
85
85
86
- If given a list or string, the initializer is passed to the new array's
87
- :meth: `fromlist `, :meth: `frombytes `, or :meth: `fromunicode ` method (see below)
88
- to add initial items to the array. Otherwise, the iterable initializer is
89
- passed to the :meth: `extend ` method.
86
+ If given a :class: `bytes ` or :class: `bytearray ` object, the initializer
87
+ is passed to the new array's :meth: `frombytes ` method;
88
+ if given a Unicode string, the initializer is passed to the
89
+ :meth: `fromunicode ` method;
90
+ otherwise, the initializer's iterator is passed to the :meth: `extend ` method
91
+ to add initial items to the array.
90
92
91
93
Array objects support the ordinary sequence operations of indexing, slicing,
92
94
concatenation, and multiplication. When using slice assignment, the assigned
@@ -152,10 +154,11 @@ The module defines the following type:
152
154
must be the right type to be appended to the array.
153
155
154
156
155
- .. method :: frombytes(s )
157
+ .. method :: frombytes(buffer )
156
158
157
- Appends items from the string, interpreting the string as an array of machine
158
- values (as if it had been read from a file using the :meth: `fromfile ` method).
159
+ Appends items from the :term: `bytes-like object `, interpreting
160
+ its content as an array of machine values (as if it had been read
161
+ from a file using the :meth: `fromfile ` method).
159
162
160
163
.. versionadded :: 3.2
161
164
:meth: `!fromstring ` is renamed to :meth: `frombytes ` for clarity.
@@ -177,7 +180,7 @@ The module defines the following type:
177
180
178
181
.. method :: fromunicode(s)
179
182
180
- Extends this array with data from the given unicode string.
183
+ Extends this array with data from the given Unicode string.
181
184
The array must have type code ``'u' `` or ``'w' ``; otherwise a :exc: `ValueError ` is raised.
182
185
Use ``array.frombytes(unicodestring.encode(enc)) `` to append Unicode data to an
183
186
array of some other type.
@@ -239,24 +242,27 @@ The module defines the following type:
239
242
240
243
.. method :: tounicode()
241
244
242
- Convert the array to a unicode string. The array must have a type ``'u' `` or ``'w' ``;
245
+ Convert the array to a Unicode string. The array must have a type ``'u' `` or ``'w' ``;
243
246
otherwise a :exc: `ValueError ` is raised. Use ``array.tobytes().decode(enc) `` to
244
- obtain a unicode string from an array of some other type.
247
+ obtain a Unicode string from an array of some other type.
245
248
246
249
247
- When an array object is printed or converted to a string, it is represented as
248
- ``array(typecode, initializer) ``. The *initializer * is omitted if the array is
249
- empty, otherwise it is a string if the *typecode * is ``'u' `` or ``'w' ``,
250
- otherwise it is a list of numbers.
251
- The string is guaranteed to be able to be converted back to an
250
+ The string representation of array objects has the form
251
+ ``array(typecode, initializer) ``.
252
+ The *initializer * is omitted if the array is empty, otherwise it is
253
+ a Unicode string if the *typecode * is ``'u' `` or ``'w' ``, otherwise it is
254
+ a list of numbers.
255
+ The string representation is guaranteed to be able to be converted back to an
252
256
array with the same type and value using :func: `eval `, so long as the
253
257
:class: `~array.array ` class has been imported using ``from array import array ``.
258
+ Variables ``inf `` and ``nan `` must also be defined if it contains
259
+ corresponding floating point values.
254
260
Examples::
255
261
256
262
array('l')
257
263
array('w', 'hello \u2641')
258
264
array('l', [1, 2, 3, 4, 5])
259
- array('d', [1.0, 2.0, 3.14])
265
+ array('d', [1.0, 2.0, 3.14, -inf, nan ])
260
266
261
267
262
268
.. seealso ::
0 commit comments