[Python-ideas] Add a datatype collections.ListView

David Mertz mertz at gnosis.cx
Fri Apr 18 02:59:08 CEST 2014


Sure, my example of initializing with an iterable isn't quite the case I
need myself, but I was just thinking of what a general-purpose collection
should do.  And actually, if I had started out using this collection, I
might have structured it as initializing with an iterable rather than
collecting my objects with list.append().


On Thu, Apr 17, 2014 at 5:21 PM, Terry Reedy <tjreedy at udel.edu> wrote:

> 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
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140417/f2ec8ce3/attachment-0001.html>


More information about the Python-ideas mailing list