tuples, index method, Python's design

Carsten Haese carsten at uniqsys.com
Tue Apr 10 09:16:00 EDT 2007


On Tue, 2007-04-10 at 12:29 +0000, Antoon Pardon wrote:
> On 2007-04-10, Carsten Haese <carsten at uniqsys.com> wrote:
> > On 10 Apr 2007 07:31:13 GMT, Antoon Pardon wrote
> >> On 2007-04-06, Carsten Haese <carsten at uniqsys.com> wrote:
> >> > If you have a use case for tuple.index, please show it to me, and I'll
> >> > show you what you should be using instead of a tuple.
> >> 
> >> No wonder no convincing use cases for tuples have shown up. You just
> >> defined use cases of tuples as unconvincing.
> >
> > It's not a definition, it's an observation. I'm simply saying that all use
> > cases I've seen for tuple.index could be written in a clearer fashion by using
> > something else. Please prove me wrong by supplying a use case of tuple.index
> > that can not be improved by rewriting.
> 
> No it is a defintion because it states this can be done for every
> possible case, even cases you have no idea about. 

That would be a theorem or conjecture, not a definition. You can call it
definition or banana or screwdriver if you like, but that's not going to
disprove it.

> > Note that I have proved elsewhere on this thread that any real use case for
> > tuple.index will involve an operation to explicitly use an index different
> > from the one obtained by the call to tuple.index(). I'd like to hear your
> > opinion on this.
> 
> And what relevance would such a proof have?

It proves at that every conceivable use case for tuple.index, including
the ones I am not thinking of, will never directly use the index
obtained from calling tuple.index(). Such a use case will be poorly
self-documented, and a clearer implementation can be found by avoiding
tuple.index().

I'll illustrate this on the one concrete use case that was suggested on
this thread: In a game where the sequence of players is given by the
tuple p, find the opponents of the current player.

One hypothetical solution is to find the index if the current player and
then slice and reassemble the tuple:

i = p.index(current_player)
opponents = p[:i-1] + p[i+1:]

An alternative is this:

opponents = tuple(x for x in p if x is not current_player)

You may disagree, but in my opinion, the alternative is better because
it is a more natural translation of the concept that the opponents of
the current player are all players that are not the current player.

> Adding the index method to tuples is not adding a feature. It is
> removing a limitation.

The non-existence of tuple.index is only a limitation if there is a need
for the method to exist. Please prove that this need exists.

-Carsten





More information about the Python-list mailing list