Slicing wrapped numpy arrays

Martin Manns mmanns at gmx.net
Sun Jan 13 18:45:50 EST 2008


On Sun, 13 Jan 2008 16:03:16 -0600
Robert Kern <robert.kern at gmail.com> wrote:

> Martin Manns wrote:
> > Hi,
> > 
> > I have created a class that wraps a numpy array of custom objects. I
> > would like to be able to slice respective objects (without copying
> > the array if possible).
> > 
> > I have browsed the doc and found some hints at __getitem__.
> > However, I still do not grasp how to do it.  How do I implement
> > __getitem__ correctly?
> 
> > mymap[10:20,15:20,:] # This line should work afterwards
> 
> The first thing you should do is simply implement a very basic,
> nonfunctional version just to see what objects come in:
> 
> In [1]: class Sliceable(object):
>     ...:     def __getitem__(self, arg):
>     ...:         print arg
>     ...:

I did that and got here:

> (slice(None, None, 2), slice(10, None, 10))

However, I still do not see how I get a Map object that employs a
slice of the array without creating a new Map object.

Martin



More information about the Python-list mailing list