[Python-ideas] Add a datatype collections.ListView

Terry Reedy tjreedy at udel.edu
Fri Apr 18 02:21:24 CEST 2014
On 4/17/2014 5:22 PM, David Mertz wrote:
> Using range objects might be worthwhile.  However, your example code,
> Terry, won't behave well if you initialize with a generic iterable.

That is why I said 'sequence', not 'iterable'. If someone ignores the 
signature of a class/function, that is his problem.

Anyway, my 'example code' was obviously an illustrative but incomplete 
outline, with missing details like docstrings, type checks, and 
exception catching left for you to fill in.

> You need the check for 'hasattr(seq, "__getitem__")' or something equivalent
> in the initializer.  But if you have that, you need to make a decision
> of *what* concrete sequence to instantiate the iterable as, with list()
> being the obvious choice.

Your presented use case is that you already have a long (concrete) 
sequence and want to take views instead of slices to save memory.

> E.g. I can do:
>
>    lv = ListView(Thing(random()) for _ in range(100000))
>
> But you can't do:
>
>    sv = SeqView(Thing(random()) for _ in range(100000))

This does not make much sense and is outside your use case.

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list