[Python-Dev] Allowing slicing of iterators
Nick Coghlan
ncoghlan at iinet.net.au
Wed Jan 26 05:40:53 CET 2005
Steven Bethard wrote:
> If we're really looking for a builtin, wouldn't it be better to go the
> route of getattr/setattr and have something like getslice that could
> operate on both lists and iterators?
Such a builtin should probably be getitem() rather than getslice() (since
getitem(iterable, slice(start, stop, step)) covers the getslice() case).
However, I don't really see the point of this, since "from itertools import
islice" is nearly as good as such a builtin.
More importantly, I don't see how this alters Guido's basic criticism that
slicing a list and slicing an iterator represent fundamentally different
concepts. (ie. if "itr[x]" is unacceptable, I don't see how changing the
spelling to "getitem(itr, x)" could make it any more acceptable).
If slicing is taken as representing random access to a data structure (which
seems to be Guido's view), then using it to represent sequential access to an
item in or region of an iterator is not appropriate.
I'm not sure how compatible that viewpoint is with wanting Python 3k to be as
heavily iterator based as 2.x is list based, but that's an issue for the future.
For myself, I don't attach such specific semantics to slicing (I see it as
highly dependent on the type of object being sliced), and consider it obvious
syntactic sugar for the itertools islice operation. As mentioned in my previous
message, I also think the iterator/iterable distinction should be able to be
ignored as much as possible, and the lack of syntactic support for working with
iterators is the major factor that throws the distinction into a programmer's
face. It currently makes the fact that some builtins return lists and others
iterators somewhat inconvenient.
Those arguments have already failed to persuade Guido though, so I guess the
idea is dead for the moment (unless/until someone comes up with a convincing
argument that I haven't thought of).
Given Guido's lack of enthusiasm for *this* idea though, I'm not even going to
venture into the realms of "+" on iterators defaulting to itertools.chain or "*"
to itertools.repeat.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
More information about the Python-Dev
mailing list