[ python-Feature Requests-992967 ] array.array objects should support sequences

SourceForge.net noreply at sourceforge.net
Mon Jul 19 21:23:53 CEST 2004


Feature Requests item #992967, was opened at 2004-07-17 13:30
Message generated for change (Comment added) made by rhettinger
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: Raymond Hettinger (rhettinger)
Date: 2004-07-19 14: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 10: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