[ python-Feature Requests-992967 ] array.array objects should
support sequences
SourceForge.net
noreply at sourceforge.net
Wed Jul 21 14:21:29 CEST 2004
Feature Requests item #992967, was opened at 2004-07-17 14:30
Message generated for change (Comment added) made by etrepum
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=992967&group_id=5470
Category: Python Library
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Bob Ippolito (etrepum)
Assigned to: Nobody/Anonymous (nobody)
Summary: array.array objects should support sequences
Initial Comment:
array objects from the array module have an extend method that
only takes arrays. The implementation would be more useful if it
accepted arbitrary sequences (generators, lists, strings, etc).
Similarly, the initializer currently only accepts lists and strings,
but it should also support arbitrary sequences.
>>> from array import array
>>> array('c', iter('1234'))
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: array initializer must be list or string
>>> array('c').extend('1234')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: can only extend array with array (not "str")
----------------------------------------------------------------------
>Comment By: Bob Ippolito (etrepum)
Date: 2004-07-21 08:21
Message:
Logged In: YES
user_id=139309
One can construct an empty list and extend it too, but for some reason
they let you make them out of arbitrary iterables. I wonder why! :)
----------------------------------------------------------------------
Comment By: Michael Chermside (mcherm)
Date: 2004-07-21 08:13
Message:
Logged In: YES
user_id=99874
I'll just chime in to agree with Raymond's analysis... the
constructor doesn't need to take general iterables. One can
always construct an empty array and extend it if that's
what's needed.
----------------------------------------------------------------------
Comment By: Raymond Hettinger (rhettinger)
Date: 2004-07-19 15:23
Message:
Logged In: YES
user_id=80475
In Py2.4, I already made extend() take a general iterable.
The goal was to simplify code doing repeated appends.
By calling array_extend, it should be easy to do the same
for the constructor. The question is whether there are use
cases to warrant a further API change. Most needs can
already be met with extend() or with array.array(code,
list(it)).
My preference is to leave the constructor for cases with a
known sequence length. With a single pre-allocation and
copy step, the performance is optimal. I don't think it
would be obvious that using a general iterable would be much
slower and consume more memory than expected. That's okay
for lists, but people use array when they are tight for
space or have other performance goals.
----------------------------------------------------------------------
Comment By: Michael Hudson (mwh)
Date: 2004-07-19 11:56
Message:
Logged In: YES
user_id=6656
I agree with your first point. Should be easy, too.
I'm /slightly/ less enthusiastic about the latter point. sequence-
>array conversion is sufficiently complex that I'm not sure I want
it happening automatically.
This is the sort of report that a patch would help immensely, btw
:-)
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=992967&group_id=5470
More information about the Python-bugs-list
mailing list