list Integer indexing dies??

Antoon Pardon apardon at forel.vub.ac.be
Thu Dec 23 10:05:20 EST 2004


Op 2004-12-23, Ishwor schreef <ishwor.gurung at gmail.com>:
> On 23 Dec 2004 14:28:37 GMT, Antoon Pardon <apardon at forel.vub.ac.be> wrote:
>> Op 2004-12-23, Ishwor schreef <ishwor.gurung at gmail.com>:
>> > Hi all. Look at this snippet of code.
>> >
>> >>>> l = ['a','b','c','d']
>> >>>> l
>> > ['a', 'b', 'c', 'd']
>> >>>> l[0][0][0]
>> > 'a'
>> > It prints the value 'a'. Fine so far :-)
>> > l[0] ---> 'a' .
>> > l[0][0]---> 'a'[0] --> 'a'.
>> > l[0][0][0] ---> 'a'[0][0] --> 'a'[0] --> 'a'
>> >
>> > Now why doesnt this list which holds integer seem to work??
>> 
>> Because this only works with strings.
>> 
>> String is the only object in python which has an implied
>> equivallence between an element and a squence of one.
>> 
>> So one character is a string and a string is a sequence
>> of characters.
>> 
>> So 'a'[0] is again 'a' which can again be indexed by
>> 0 as many times as you want.
>
> ;-) gotcha. But shouldn't this be valid too?? 
>>>> 123232[0] 

Well if it should become valid, it should just return 123232 IMO.

> in which basically python can infer from the object type and print out
> 1 instead of coughing up those errors?

Why do you feel it should cough up 1?

Suppose I write a number in octal notation.

What should 035[0] cough up? Be carefull it should
cough up the same as 29[0].

> My experience as a learner here
> is that there should be some automagics  & say like "okay you want to
> do indexing on integers ( context dependent); i'll give you the index
> of 0th position in that integer" ???

Integers have no position. The position we think of in integers is an
artefact of our notational system. Even then if we would simply defer
to decimal notation there is still a problem. You see there are a
number of arguments that would make 123232[0] cough up 2. Because
by starting indexing from the back we get a nice correspondence between
the index of the number and the power of 10 it represents.

-- 
Antoon Pardon



More information about the Python-list mailing list