Tuple question

Roy Smith roy at panix.com
Thu Sep 2 15:52:45 EDT 2004


In article <mailman.2783.1094152072.5135.python-list at python.org>,
 Gandalf <gandalf at geochemsource.com> wrote:

> > Please see these recent threads, and read the FAQ:
> >
> > http://groups.google.ca/groups?threadm=mailman.2685.1093979591.5135.python-l
> > ist%40python.org 
> 
> 
> This is from that thread:
> 
> >Note that while .index() makes sense for some sequences,
> >such as strings and lists, it doesn't make sense for the
> >way in which tuples are "supposed to be used", which is
> >as collections of heterogeneous data and not usually as
> >simply read-only lists. 
> 
> Why it is not useful to have an index() method for collections of 
> heterogeneous data?
> 
> Suppose, you have big amount of data stored in tuples (for using less 
> memory).
> You may want to extract slices from the tuploes from a given index determined 
> by an object.
> This is just an example, however it is quite realistic (e.g. using tuples 
> instead of lists
> because there is a huge amount of static data that you need to access 
> quickly).

Also, you must use tuples instead of lists as dictionary keys.  If your 
keys are inherently arbitrary length ordered collections of homogeneous 
data, you might very well want to use things like index() on the keys.  
In this case, they really are "immutable lists".

I understand the argument that tuples are supposed to be the moral 
equivalent of anonymous C structs, but if that's the case, why do things 
like len() and slicing work on them?  Not to mention "in" (either as a 
test or as an iterator).  None of those things make sense to do on 
structs.

It's really pretty arbitrary that of the things you can do on immutable 
sequences, index() and count() are special-cased as innapropriate 
operations for tuples.

Looking over the Zen list, I'd say any of:

Simple is better than complex.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.

argue for allowing index() and count() to be used on tuples.



More information about the Python-list mailing list