On Thu, Mar 28, 2002, Guido van Rossum wrote:
Very confusing. I propose to remove the start/stop arguments, *or* change the spec to:
def iterindexed(sequence, start=0, stop=None): i = start while stop is None or i < stop: try: item = sequence[i] except IndexError: break yield (i, item) i += 1
This reduces the validity to only sequences (as opposed to all iterable collections), but has the advantage of making iterindexed(x, i, j) iterate over x[i:j] while reporting the index sequence range(i, j) -- not so easy otherwise.
The simplified version is still attractive because it allows arbitrary iterators to be passed in:
def iterindexed(collection): i = 0 it = iter(collection) while 1: yield (i, it.next()) i += 1
How about doing it both ways: if you try to pass start/stop for an iterator instead of a sequence, you get an AttributeError on __getindex__. (I'm not proposing this, just throwing it out as an idea. It does make explaining it more difficult, which is an argument against.) -- Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/ Why is this newsgroup different from all other newsgroups?