tuple.index()

Carsten Haese carsten at uniqsys.com
Thu Dec 14 11:15:17 EST 2006


On Thu, 2006-12-14 at 15:57 +0000, Nick Maclaren wrote:
> In article <mailman.1605.1166111433.32031.python-list at python.org>,
> "Fredrik Lundh" <fredrik at pythonware.com> writes:
> |> 
> |> > The point is that an index method makes sense on ANY data structure that
> |> > can be subscripted by an integer value but, for reasons that aren't at
> |> > all clear, is not defined for Python tuples.  There is no technical or
> |> > mathematical reason why it shouldn't be.
> |> 
> |> so where would you put such an "index" implementation so it would work on
> |> ANY data structure that can be subscripted by an integer value ?
> 
> In the same place you put the subscription method, clearly.

Clearly not, because that would force *every* object that implements
__getitem__ to also implement index.

And to verify the truth of your assertion that index makes sense for
"ANY data structure that can be subscripted by an integer value",
consider the following theoretical but conceivable examples:

1) A mailbox object that returns the i-th message on subscripting.
2) A scroll cursor in a database that returns the i-th row of the result
set.
3) A prime number generator that calculates the i-th prime number.

Does .index() make sense for them? How would it be implemented?

-Carsten





More information about the Python-list mailing list