Why Python does *SLICING* the way it does??

Dan Bishop danb_83 at yahoo.com
Thu Apr 21 02:59:06 EDT 2005


beliavsky at aol.com wrote:

> What languages besides Python use the Python slicing convention?

Java uses it for the "substring" method of strings.

> In C starting at
> 0 may be justified because of the connection between array
subscripting
> and pointer arithmetic, but Python is a higher-level language where
> such considerations are less relevant.

Maybe less relevant, but relevant nonetheless.

First, there's the desire for familiarity.  Many Python programmers are
also C programmers, and that fact has had an influence on the
development of the language.  That's why we write "x += 0x20" rather
than "x := x + $20".  Why not array indexing as well?

More importantly, there are reasons for counting from zero that have
nothing to do with pointers.

The biggest reason involves modular arithmetic: r=n%d satifies 0 <= r <
d, which conveniently matches Python's array syntax.

   DAY_NAMES = ["Sunday", "Monday", "Tuesday", "Wednesday",
                "Thursday", "Friday", "Saturday"]
   def weekday_name(date):
      return DAY_NAMES[date.toordinal() % 7]

Modular arithmetic's preference for 0-based counting goes beyond array
indexing.  For example, consider our notation for time, from 00:00:00
to 23:59:59.

   def time_add(time, delta):
      """
      time  = an (hour, minute, second) tuple for a time of day
      delta = an (hour, minute, second) tuple for an amount of time

      Returns time+delta, as an (hour, minute, second) tuple.
      """
      hour = time[0] + delta[0]
      minute = time[1] + delta[1]
      second = time[2] + delta[2]
      # Normalize the time
      second = ((hour * 60) + minute) * 60 + second
      minute, second = divmod(second, 60)
      hour, minute = divmod(minute, 60)
      hour %= 24
      return hour, minute, second

Imagine that the time notation went from 01:01:01 to 24:60:60.  Try
writing a time_add function for that.  The only simple way I can think
of is to temporarily convert to zero-based notation!

   def time_add(time, delta):
      # Add like terms and convert to zero-based notation.
      hour = time[0] + delta[0] - 1
      minute = time[1] + delta[1] - 1
      second = time[2] + delta[2] - 1
      # Normalize the time
      second = ((hour * 60) + minute) * 60 + second
      minute, second = divmod(second, 60)
      hour, minute = divmod(minute, 60)
      hour %= 24
      # Convert back to one-based notation on output
      return hour + 1, minute + 1, second + 1

> Along the same lines, I think the REQUIREMENT that x[0] rather than
> x[1] be the first element of list x is a mistake. At least the
> programmer should have a choice, as in Fortran or VBA.

If you really want 1-based arrays, just do what most BASIC programmers
do: Ignore x[0].

>>> months = [None, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec"]
>>> print months[4]
Apr




More information about the Python-list mailing list