Why are slice indices the way they are in python?

ogbash at gmail.com ogbash at gmail.com
Thu Nov 30 11:58:56 EST 2006


> Why?  It doesn't seem intuitive to me.  To me, it makes it harder, not
> easier, to work with slices than if indexing started at 1 and  the
> above expression got you the 2nd throught the 5th character.

Zero-based indices and excluding last index often works nicer, it is
not the rule, but all my experience says that.

a[i:i] is very handy to have in many algorithms, so you don't have to
exclude this case explicitly.
Index starting at 1 or including N often adds +-1 expressions to the
code. I think that http://en.wikipedia.org/wiki/Modular_arithmetic
makes it more intuitive.

And if talking about dates, then I suggest NEVER use 2006-12-31
23:59:59 in data, always 2007-01-01 00:00:00 instead. If customer wants
to see former on the screen just substruct 1 millisecond from the data
and round it to what is needed. It makes all arithmetics, finding
period collapses, time differences, etc much more painless. You
sometimes don't even know if your database supports seconds,
milliseconds, microseconds and what your operating system and
programming language support. So if you are using inclusive end date
you may find yourself in trouble very soon.

Again, its not intuitive for ordinary people, but math people and
programmers often find 0-based and exlusive ends are nicer to work
with, at least me :)

> Another thing that I've been puzzling over is the pow() function.
>
> pow(x,y) gives x**y.  Fine.
>
> But pow(x,y,z) gives (x**y) % c
>
> I'm curious to know what the pressing reason for such a feature was.
This makes cryptography code 100 times faster.

Oleg




More information about the Python-list mailing list