tuples, index method, Python's design
OKB (not okblacke)
brenNOSPAMbarn at NObrenSPAMbarn.net
Mon Apr 9 14:01:44 EDT 2007
Carsten Haese wrote:
> I won't presume to speak for Guido, but if I had to connect the
> dots between "tuples are immutable" and "tuples will rarely need an
> index method", I'd do it like this: Consider an object X and a
> tuple T. Tuples have "X in T" tests, so you can check if there
> exists some N such that T[N]==X. The lack of the index method means
> you won't know the value of N, but what would knowing N give you?
> You can't modify T, so all you can do is read T[N], but you already
> know that that's going to return X, so you might as well just use
> X.
>
> Any use case for actually having to know N would have to involve an
> operation where you end up explicitly using an index other than N,
> such as slicing T from 0 to N-1, or looking at T[2*N] or something
> like that. Of course such operations might be useful, but
> algorithms that need an operation like that would self-document
> their design more clearly by using a list instead of a tuple.
This is a reasonable argument, but all such arguments miss what I
think is an important use case for tuples: you want a list, but you need
to use it as a dictionary key.
--
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is
no path, and leave a trail."
--author unknown
More information about the Python-list
mailing list