[PYTHON MATRIX-SIG] Slices

Konrad HINSEN hinsenk@ere.umontreal.ca
Thu, 28 Mar 1996 09:11:34 -0500


The recently introduced slice notation (a:b etc.) has greatly improved
readability of slice indexing, but is has also lead to the disappearance
of the old Slice() objects (they are still there, but are not accepted
as indices any more) and thereby important functionality: it is no
longer possible to construct index expressions containing slices
dynamically.

For those who wonder why anyone might want to do this, I'll describe
my application: I have written a class that represents a function
defined on a grid, with linear interpolation for arguments between the
grid points. Of course it should work for functions of any number of
arguments. A crucial step in the interpolation is the extraction of
the subarray of neighbouring grid points, i.e. a subarray of length 2
along any axis. It is no problem to calculate the first/last indices
for each axis, but there is currently no way to construct an indexing
expression with slices, because the : notation is legal only inside
square brackets.

There are basically three possible solutions:

1) Make the : notation legal everywhere. That would make slice objects
   first-class objects like anything else in Python. You could create
   them, assign them to variables, make them elements of lists, tuples,
   or arrays, etc. It would then make sense to make them real sequence
   objects that can be subscripted, used in for-loops, converted to
   lists or tuples etc.

2) Reintroduce the old Slice() notation for the new slice objects.
   The consequences would be the same as above, but with a less
   uniform syntax. I don't really see the point of doing this.

3) Keep slice objects restricted to index expressions, but create
   some way to make them act on a dynamically specified axis. Then
   the example given above could be handled by iteratively indexing
   along the axes. Less elegant, but better than nothing.

My personal favourite is 1), and I can't see any disadvantage in
this. But that makes me think about why slice objects were restricted
to subscripts in the first place. Was there a good reason for that, or
was it just the desire to make the smallest possible change to Python
syntax?

-------------------------------------------------------------------------------
Konrad Hinsen                     | E-Mail: hinsenk@ere.umontreal.ca
Departement de chimie             | Tel.: +1-514-343-6111 ext. 3953
Universite de Montreal            | Fax:  +1-514-343-7586
C.P. 6128, succ. Centre-Ville     | Deutsch/Esperanto/English/Nederlands/
Montreal (QC) H3C 3J7             | Francais (phase experimentale)
-------------------------------------------------------------------------------

=================
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
=================