SV: SV: [Tutor] Why use tuples?

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Thu, 20 Sep 2001 15:02:48 -0700 (PDT)


On Thu, 20 Sep 2001, Ignacio Vazquez-Abrams wrote:

> > | > Ok. But then again I can not use a tuple as an index which
> > | would have been natural for me to be able to with a datatype of
> > | this nature. So does the statement
> > | >
> > | > if col[ColType] == IU32:
> > | >
> > | > where ColType is a tuple does not work. It is counter intuitive
> > | but I guess I have to get used to not using tuples.
> > |
> > | Have you _tried_ using a tuple as an index?
> >
> > Yes, in the above example. It didn't work out. Your example below is with a dictionary. I understand that it works there. But does it work generally thorughout Python and if so, how do I get it to work? I expected that it would make no difference to Python if ColType is an integer, a list element or a tuple. If this is so, please enlighten me.
> 
> Ah, I see what you mean.
> 
> All sequence types (lists, tuples, or strings) can only use integers
> or slices for indexes. Dictionaries can use any immutable type
> (integers, strings, or tuples).


Yes.  One way we can see this is by trying:

###
>>> train_station = range(11)
>>> train_station
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> train_station[9.75]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: sequence index must be integer
###

It's very difficult to get to train_station Nine and Three Quarters
without magic...