negative indices for sequence types

Istvan Albert ialbert at mailblocks.com
Mon Sep 8 03:00:41 CEST 2003


dan wrote:

> This fact is *deeply* buried in the docs, and is not at all intuitive.
>  One of the big advantages of a high-level language such as Python is
> the ability to provide run-time bounds checking on array-type
> constructs.

Bounds checking means that the size is tracked for you and
an exception is thrown if you are trying to access an
element *beyond* that size. That's the natural way
of thinking about it, and not "checking wether there
is an index like this in the list".

The python way of using negative numbers in indices is
extremly handy as many have pointed out. It would be
silly to forego all that expressivness just to save an
if test in some rare cases.

 > To achieve this I will now have to subclass my objects
 > and add it myself, which seems silly and will add significant
 > overhead.

I would guess that instead of paying for this every time,
as you want to (subclassing), you could just as simply
check the index at the time when you generate it and verify
that it is correct. This way using the same list in differnt
context will not make it less efficient.

Istvan.





More information about the Python-list mailing list