Python indexing logic

I am wondering in Python why it x[0]

is retrieving the first element x

and x[-1]

retrieving the first element when reading in reverse order. The syntax seems incompatible to me, as in one case we are calculating the distance from the first element, whereas we are not counting the distance from the last element when reading back. Doesn't something like x[-0]

that make sense? One thought I have is that spacing in Python is usually considered to be included relative to the bottom border, but exclusively for the top border, and therefore the index can be interpreted as the distance from an element with a bottom or top border. Any ideas on why this notation was chosen? (I'm also just wondering why null indexing is preferred at all.)

+3


source to share


4 answers


The case for zero-based indexing in general is briefly described by Dijkstra here . On the other hand, you need to think about how Python array indices are calculated. When array indices are first computed:

x = arr[index]

      

will resolve and evaluate first index

, and -0

obviously evaluates as 0

, it would be impossible to have arr[-0]

to indicate the last element.

y = -0 (??)
x = arr[y]

      

hardly makes sense.

EDIT:



Let's look at the following function:

def test():
    y = x[-1]

      

Let's assume what x

was announced above in the global scope. Now let's take a look at the bytecode:

          0 LOAD_GLOBAL              0 (x)
          3 LOAD_CONST               1 (-1)
          6 BINARY_SUBSCR
          7 STORE_FAST               0 (y)
         10 LOAD_CONST               0 (None)
         13 RETURN_VALUE

      

Basically, a global constant x

(or rather its address) is pushed onto the stack. Then the index of the array is calculated and pushed onto the stack. Then the instruction BINARY_SUBSCR

that implements TOS = TOS1[TOS]

(where TOS

means Top of Stack

). Then the top of the stack is inserted into the variable y

.

Since it BINARY_SUBSCR

handles negative array indices and what -0

will be evaluated before 0

before being pushed to the top of the stack, it would require significant changes (and unnecessary changes) for the interpreter to arr[-0]

indicate the last element of the array.

+4


source


This is mainly due to several reasons:

  • Computers work with 0-bit numbers
  • Older programming languages ​​used 0-based indexing because they were low-level and closer to machine code.
  • Newer, higher-level languages ​​use it for consistency and for the same reasons.


For more information: https://en.wikipedia.org/wiki/Zero-based_numbering#Usage_in_programming_languages

+1


source


In many other languages ​​that use 0-based indexes but no negative index, implemented like python, accessing the last element of a list (array) requires finding the length of the list and subtracting 1 for the last element, e.g .:

items[len(items) - 1]

      

In python the part len(items)

can simply be omitted with negative index support, consider:

>>> items = list(range(10))
>>> items[len(items) - 1]
9
>>> items[-1]
9

      

+1


source


In python:, 0 == -0

therefore x[0] == x[-0]

.

Why is the zero indexing sequence not based on the same base? This is a choice the language designer must make. Most languages ​​that I know use 0-based indexing. Xpath uses 1 for selection.

The use of negative indexing is also conditional for the language. Not sure why it was chosen, but it allows you to loop or loop through a sequence by simply adding (subtracting) by index.

0


source







All Articles