@@ -76,14 +76,16 @@ The module defines the following type:
76
76
.. class :: array(typecode[, initializer])
77
77
78
78
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.
82
82
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.
87
89
88
90
Array objects support the ordinary sequence operations of indexing, slicing,
89
91
concatenation, and multiplication. When using slice assignment, the assigned
@@ -149,10 +151,11 @@ The module defines the following type:
149
151
must be the right type to be appended to the array.
150
152
151
153
152
- .. method :: frombytes(s )
154
+ .. method :: frombytes(buffer )
153
155
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).
156
159
157
160
.. versionadded :: 3.2
158
161
:meth: `!fromstring ` is renamed to :meth: `frombytes ` for clarity.
@@ -174,9 +177,9 @@ The module defines the following type:
174
177
175
178
.. method :: fromunicode(s)
176
179
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
180
183
array of some other type.
181
184
182
185
@@ -236,23 +239,27 @@ The module defines the following type:
236
239
237
240
.. method :: tounicode()
238
241
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' ``;
240
243
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.
242
245
243
246
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
248
253
array with the same type and value using :func: `eval `, so long as the
249
254
: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.
250
257
Examples::
251
258
252
259
array('l')
253
260
array('u', 'hello \u2641')
254
261
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 ])
256
263
257
264
258
265
.. seealso ::
0 commit comments