R
Roberto A. F. De Almeida
I found that when using negative indices, the slice object passed to
__getitem__ depends on the number of slices. An example to clarify:
class a:
def __getitem__(self, index):
return index
File "<stdin>", line 1, in ?
AttributeError: a instance has no attribute '__len__'
But if I pass a "multiple" slice:
If we add the following __len__ to class a:
def __len__(self):
return 42
Then the "single" slice works based on __len__:
But not for the "multiple" slice:
I am having these problems because I want to slice a multi-dimensional
object. When slicing the object, __getitem__ returns a Numeric array
of the desired size:
Is there something wrong in using slices like this in objects? A
better way?
__getitem__ depends on the number of slices. An example to clarify:
class a:
def __getitem__(self, index):
return index
Traceback (most recent call last):b = a()
print b[:-1]
File "<stdin>", line 1, in ?
AttributeError: a instance has no attribute '__len__'
But if I pass a "multiple" slice:
(slice(None, -1, None), slice(None, -1, None))print b[:-1,:-1]
If we add the following __len__ to class a:
def __len__(self):
return 42
Then the "single" slice works based on __len__:
slice(0, 41, None)print b[:-1]
But not for the "multiple" slice:
(slice(None, -1, None), slice(None, -1, None))print b[:-1,:-1]
I am having these problems because I want to slice a multi-dimensional
object. When slicing the object, __getitem__ returns a Numeric array
of the desired size:
-1728print dataprint data.variables['u'].shape (16, 17, 21)
u = data.variables['u'][:,:,:]
print type(u)print u.shape (16, 17, 21)
u[0,0,0]
Is there something wrong in using slices like this in objects? A
better way?