osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Python-Dev] Replacement for array.array('u')?


On Fri, 22 Mar 2019 12:51:49 +0100
Stefan Behnel <stefan_ml at behnel.de> wrote:
> Antoine Pitrou schrieb am 22.03.19 um 11:39:
> > On Fri, 22 Mar 2019 20:31:33 +1300 Greg Ewing wrote:  
> >> A poster on comp.lang.python is asking about array.array('u').
> >> He wants an efficient mutable collection of unicode characters
> >> that can be initialised from a string.  
> > 
> > TBH, I think anyone trying to use array.array should be directed to
> > Numpy these days.  The only reason for array.array being here is that
> > it predates Numpy.  Otherwise we'd never have added it.  
> 
> Well, maybe it wouldn't get *added* these days anymore, with pip+PyPI
> nicely in place. But being there already, it makes for a nice and efficient
> "batteries included" list replacement for simple data that would otherwise
> waste a lot of object memory.

It's not really "batteries included".  array.array() supports almost no
useful operation. It's a bare-bones container for which you have to
implement every useful feature by yourself.

(yes, you can use generic mutable sequence algorithms such
as heapq or random.shuffle; how often do you need to heapify or shuffle
an array of unicode codepoints?)

Also, when using a unicode array, there's no substantial win of memory
compared to a single str object.  You may be losing some actually,
because of the flexible str representation.

Regards

Antoine.