[Python-Dev] iter.index()

Bob Ippolito bob at redivi.com
Sun Apr 18 22:50:13 EDT 2004


On Apr 18, 2004, at 10:21 PM, Christian Stork wrote:

> On Sun, Apr 18, 2004 at 10:08:25PM -0400, Bob Ippolito wrote:
>>
>> On Apr 18, 2004, at 9:53 PM, Christian Stork wrote:
>>
>>> Hi,
>>>
>>> I wanted to do something like
>>>
>>>    i = itertools.chain(list1, list2).index(elem)
>>>
>>> but that gave me
>>>
>>>    ...
>>>    AttributeError: 'itertools.chain' object has no attribute 'index'
>>>
>>> If I use the operator module it works just fine.
>>>
>>>    i = operator.indexOf(itertools.chain(list1, list2), elem)
>>>
>>> Why not add the index method to iterator objects?  Of course,
>>> internally
>>> only next() is used by the default implementation.
>>
>> An iterator mutates each time you call its next().  Your call to
>> indexOf does one of three things: exhaust some of your iterator and
>> return a useless integer,
>
> Why useless?  It returns the index.  Why do I have to build a new
> concatinated list (or my own special index function) in order to get
> that information?

It returns an index into some sequence that no longer exists.  What 
good is that?

>> exhaust all of your iterator and raise an exception,
>
> Yep, just as it would for lists.

The difference is that the iterator's life is more or less over, 
there's probably nothing useful you can do with it, but a list is not 
changed by this operation.

>> If you want an object that acts like a list, you should use a list.
>
> I don't see anything inherently "listy" about index().  It just counts
> how many elements there are to reach elem.  And obviously the
> functionality is already there in the operator module.  I'm just
> proposing a little convenience.

I think it's pretty rare that you would want to know this information 
at the cost of exhausting some/all of your iterator... and if that 
really is what you want, then you should just use operator.indexOf.  
There are MANY iterable types, it's not reasonable to change them all.

-bob




More information about the Python-Dev mailing list