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

How would you replicate the behavior of __exit__ in Christian's example?
Why would I want to replicate it? Eli

On 03/05/2013 11:44 AM, Eli Bendersky wrote:
I suspect the new behavior would be most useful when you don't know precisely how large the final list will be: overallocate (possibly by a large margin), then __exit__ returns the unused portion back to the pool). -- ~Ethan~

On 3/5/2013 2:50 PM, Ethan Furman wrote:
If one counts the items as they are added, it is easy to delete extras. ll = [None]*200 for i in range(195): ll[i] = i del ll[195:] ll[190:] # [190, 191, 192, 193, 194] del ll[ll.index(None):] would do the same thing and be faster than counting one by one. -- Terry Jan Reedy

On Tue, Mar 5, 2013 at 8:06 PM, Terry Reedy <tjreedy@udel.edu> wrote:
Length hints can lie, so there's a possibility of underallocation. So you need an additional check to use use ll.append instead -- Philip Jenvey

On 03/05/2013 11:44 AM, Eli Bendersky wrote:
I suspect the new behavior would be most useful when you don't know precisely how large the final list will be: overallocate (possibly by a large margin), then __exit__ returns the unused portion back to the pool). -- ~Ethan~

On 3/5/2013 2:50 PM, Ethan Furman wrote:
If one counts the items as they are added, it is easy to delete extras. ll = [None]*200 for i in range(195): ll[i] = i del ll[195:] ll[190:] # [190, 191, 192, 193, 194] del ll[ll.index(None):] would do the same thing and be faster than counting one by one. -- Terry Jan Reedy
participants (5)
-
Eli Bendersky
-
Ethan Furman
-
Philip Jenvey
-
Serhiy Storchaka
-
Terry Reedy