[MATRIX-SIG] Change slice object?

Konrad Hinsen hinsen@ibs.ibs.fr
Mon, 24 Nov 1997 12:52:24 +0100


>  > > Also then there is no need for __getitem__ if one uses indexing. But I
>  > > think the method is needed for the for-loop construct, right?
>  > 
>  > Right. And also for indexing with a single number, e.g. a[5].
>  > 
> My idea was that exactly this kind of indexing should also be done by 
> __getslice__, with the syntax slice(5,6,None). Currently one gets a

But what good would this be, as long as the interpreter always calls
__getitem__ for single number indices? And that can't be changed
without creating backwards compatibility problems.

On the other hand, it's nearly trivial to write an implementation of
__getitem__ that simple calls __getslice__ with the right arguments.

> This is truly the bad side of such a change, so I will try to build a
> class wich can simplify the handling of indices. First I make a
> shadow-class for slice (see end of this mail) and than a
> composer class wich handles the index argument of __get{slice,item}__.

That's close to how I do C implementations of indexing (see e.g.
the netCDF module). The interface functions called by the Python
interpreter just create a unified representation of the index request,
and the real work is done by a single function for all types of
indexing.

>  > > One other idea. What's the way to have a __cmp__ method for slice
>  > > objects to test if a slice is completly in another slice or
>  > > overlapping at both or only one end.
>  > 
>  > That could easily be done, but what would it be good for?
> I cache array slices from netcdf-files and want to see, if a new
> request can be taken from the cached arrays or must be loaded from the
> file.

OK, that makes sense. Adding comparison and hashing would make
slice objects suitable for all kinds of list and dictionary
operations.

> I have a class wich represents a region. I can slice the region and
> the get variables in that region. I catch the request in __getattr__
> and load or calculate the desired variable of that region. For the
> user this looks like data-access, because she gets an arrayobject. Or
> should something like this be implemented with special functions, to
> make it clear, that there is something happening?

If something looks like an attribute, then it should act like
an attribute, i.e.:

- it should always return the same value, unless the attribute
  has been changed in the meantime

- changes should be possible by assignments (unless the attributes
  are read-only, of course, in which case an exception should be
  raised for an attempted assignment)

Within these limits, it's a pure implementation decision how the
data is obtained.
-- 
-------------------------------------------------------------------------------
Konrad Hinsen                          | E-Mail: hinsen@ibs.ibs.fr
Laboratoire de Dynamique Moleculaire   | Tel.: +33-4.76.88.99.28
Institut de Biologie Structurale       | Fax:  +33-4.76.88.54.94
41, av. des Martyrs                    | Deutsch/Esperanto/English/
38027 Grenoble Cedex 1, France         | Nederlands/Francais
-------------------------------------------------------------------------------

_______________
MATRIX-SIG  - SIG on Matrix Math for Python

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