negative indices for sequence types

dan danbmil99 at yahoo.com
Sun Sep 7 14:26:28 EDT 2003


I was recently surprised, and quite shocked in fact, to find that
Python treats negative indices into sequence types as if they were
mod(length-of-sequence), at least up to -len(seq).

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.  To achieve this I will now have to subclass my objects
and add it myself, which seems silly and will add significant
overhead.  If you want this behavior, how hard is it to say a = b[x %
len(b)] ??

Can anyone explain why this anomaly exists, and why it should continue
to exist?




More information about the Python-list mailing list