Status of PEP's?

Raymond Hettinger python at rcn.com
Sun Mar 3 23:34:42 EST 2002


"Greg Ewing" <greg at cosc.canterbury.ac.nz> wrote in message
news:3C82C91C.A60B3242 at cosc.canterbury.ac.nz...
> "Hernan M. Foffani" wrote:
> >
> > But I fail to see how to apply this solution naturally on sets that have
> > several different main 'walking' directions like arrays or like the
sample
> > in the PEP. If the 'table' object has symmetry between two (or more)
axes,
> > then
> >         indici(table)
> > is ambiguous.
>
> An optional second argument indicating which dimension
> you want the indices for.
>
>   for i in indices(myarray, 0):
>     for j in indices(myarray, 1):
>       ...

No, please.

Even for symmetric tables, a default direction should be assigned
(say, row-major).  Then, the normal pythonic approach to nested
lists or tuples can be used.

For the most common case of rectangular tables (all rows have the
same length and all columns have the same length), forget the new
notation, save the repeated calls to len(), and use just one sample len()
from each dimension (then loop row-major or column-major as you
please):

rowcnt, colcnt = len(arr), len(arr[0])
for i in range(rowcnt):
   for j in range(colcnt):
       someOp( arr[i][j] )

For the less common case of irregular tables, the proposed dimension
argument does weird things.  Given a ragged right, what would looping
over columns do with:
  10  20  30
  40  50  60  70 80
  90  11  12  13

Let's use the optional argument for something different:
    indices( anIterable, [start, [stop]] )
Now, you can make friends with people who number their
arrays starting at one instead of zero ;)

Also, indices() and indexed() should return a generator instead
of a fully evaluated list.  Why eat-up memory unnecessarily.

One final note, if indexed() is available, why would indices()
ever be needed?  I can't think of any useful examples where the
index is useful but the index reference is never fetched.  This
contrasts with dictionaries where there are examples of
needing keys but not needing the values.


Raymond Hettinger







More information about the Python-list mailing list