[Python-ideas] Length hinting and preallocation for container types

Antoine Pitrou solipsis at pitrou.net
Sat Mar 9 19:19:22 CET 2013

On Sat, 09 Mar 2013 03:37:13 +1100
Steven D'Aprano <steve at pearwood.info> wrote:
> On 09/03/13 00:27, Eli Bendersky wrote:
> > If it's voting time, I'm -1. Having programmed a lot of memory-constrained
> > systems (not in Python, though) - this is not how things usually work
> > there. In a memory-constrained system, you don't "grow and shrink" your
> > data structures. That's because growing often needs to reallocate the whole
> > chunk and do a copy, and shrinking only helps memory fragmentation. In such
> > systems, you usually know in advance or at least limit the size of data
> > structures and pre-allocate, which is perfectly possible in Python today.
> Are you referring to using (say) [None]*n, for some size n?
> Is this a language guarantee that it won't over-allocate, or just an accident
> of implementation?

Probably an accident of implementation, but I can't think of any reason
to over-allocate here, so probably all implementations allocate exactly.



More information about the Python-ideas mailing list