index() of sequence type?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Feb 7 16:57:07 EST 2008


En Thu, 07 Feb 2008 11:31:44 -0200, Diez B. Roggisch <deets at nospam.web.de>  
escribió:

>> I see list has index member, but is there an index function that applies
>> to any sequence type?
>>
>> If not, shouldn't there be?
>
> Looks like an oversight to me as well, yes. The only "difficult"
> implementation would be the one for xrange, because you can't search but
> must compute the result - but that should be trivial.

xrange is iterable, but not a sequence. Tuples are worse: they implement  
__contains__ but not index. So you can say:

py> 2 in (1,2,4,8)
True

but not:

py> (1,2,4,8).index(2)

Given that to implement __contains__ it has to scan the values the same  
way as index would do, it's like a tuple saying: "I know where that item  
is, and you know that I know that, but I won't tell you!" - rather  
frustrating.

-- 
Gabriel Genellina




More information about the Python-list mailing list