I had actually not thought about the question of what should happen when
performing multiple index operations on the same iterator, and maybe that's a
reason that the idea of adding index lookup using brackets is not as good as it
first seems.
The whole point of adding that would be to reduce the number of situations in
which it matters whether you have a sequence, or and iterator. As soon as we
consider what should happen for multiple index lookups on a single iterator,
that concept breaks down.
The next thing that makes me think of that's even farther afield from the
initial topic of this thread would be to have some new function in the standard
library that is similar to 'islice' but returns an array instead of a new
iterator and performs optimally when given a list or tuple as an argument.
Maybe it could be named something like 'gslice', short for "greedy slice".
Hypothetical simplistic implementation:
def gslice(source, start_or_stop=None, stop=None, step=None):
if isinstance(source, collections.abc.Sequence):
return source[slice(start_or_stop, stop, step)]
elif isinstance(source, collections.abc.Iterable):
return list(islice(start_or_stop, stop, step))
else:
raise TypeError("'source' must be a sequence or iterable")
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/RRSJ65RYDRJ2X4K235M4M4AYJSTQAINB/
Code of Conduct: http://python.org/psf/codeofconduct/