Initializing a list with copies
Ralf Juengling
juenglin at informatik.uni-freiburg.de
Tue Apr 30 12:47:31 EDT 2002
Alex Martelli <aleax at aleax.it> writes:
> Ralf Juengling wrote:
> ...
> > BTW: It is not easy to add __iter__ to list's meta-type, is it?
>
> And giving list a different metaclass than all the other types
> would no doubt be weird.
agree.
> Further, it would be utterly weird if 'take' was introduced,
> operated correctly on iterables (including iterators), but
> returned a list rather than an iterator! It would freak me
> out... we finally have a 'lazy' paradigm, and we'd just
> throw it away like this?! take and drop should both accept
I see what you mean from what I left out (i.e. that your version
of take(n, it) yields an iterator that raises StopIteration at the
latest after n calls to it.next()).
However, I think it's good idea to let take() yield a list of
elements and drop() an iterator. Why this difference? Iterators
are potentially "lists whithout end". But they always have *one*
tail, i.e. a beginning.
I think of take() as of a convenient way to 'take' some elements
from the beginning of the list at once (which I would do otherwise
by repetitive calls the iterators next method) and of drop() as of
a convenient way to drop some of these elements I'm not interested
in.
So we wouldn't through away the 'lazy paradigm' with a take()
that yields a list. We would just use take() in cases where we
actually want some elements at once, e.g. here:
> >>> List2D = list(take(5, iter(list, '')))
> >>> List2D
> [[], [], [], [], []]
> >>>
Ralf
More information about the Python-list
mailing list