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__(sel f, index):
return index
[color=blue][color=green][color=darkred]
>>> b = a()
>>> print b[:-1][/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: a instance has no attribute '__len__'
But if I pass a "multiple" slice:
[color=blue][color=green][color=darkred]
>>> print b[:-1,:-1][/color][/color][/color]
(slice(None, -1, None), slice(None, -1, None))
If we add the following __len__ to class a:
def __len__(self):
return 42
Then the "single" slice works based on __len__:
[color=blue][color=green][color=darkred]
>>> print b[:-1][/color][/color][/color]
slice(0, 41, None)
But not for the "multiple" slice:
[color=blue][color=green][color=darkred]
>>> print b[:-1,:-1][/color][/color][/color]
(slice(None, -1, None), slice(None, -1, None))
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:
[color=blue][color=green][color=darkred]
>>> print data[/color][/color][/color]
<opendap.client .dataset instance at 0x406304ec>[color=blue][color=green][color=darkred]
>>> print data.variables['u'].shape[/color][/color][/color]
(16, 17, 21)[color=blue][color=green][color=darkred]
>>> u = data.variables['u'][:,:,:]
>>> print type(u)[/color][/color][/color]
<type 'array'>[color=blue][color=green][color=darkred]
>>> print u.shape[/color][/color][/color]
(16, 17, 21)[color=blue][color=green][color=darkred]
>>> u[0,0,0][/color][/color][/color]
-1728
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__(sel f, index):
return index
[color=blue][color=green][color=darkred]
>>> b = a()
>>> print b[:-1][/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: a instance has no attribute '__len__'
But if I pass a "multiple" slice:
[color=blue][color=green][color=darkred]
>>> print b[:-1,:-1][/color][/color][/color]
(slice(None, -1, None), slice(None, -1, None))
If we add the following __len__ to class a:
def __len__(self):
return 42
Then the "single" slice works based on __len__:
[color=blue][color=green][color=darkred]
>>> print b[:-1][/color][/color][/color]
slice(0, 41, None)
But not for the "multiple" slice:
[color=blue][color=green][color=darkred]
>>> print b[:-1,:-1][/color][/color][/color]
(slice(None, -1, None), slice(None, -1, None))
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:
[color=blue][color=green][color=darkred]
>>> print data[/color][/color][/color]
<opendap.client .dataset instance at 0x406304ec>[color=blue][color=green][color=darkred]
>>> print data.variables['u'].shape[/color][/color][/color]
(16, 17, 21)[color=blue][color=green][color=darkred]
>>> u = data.variables['u'][:,:,:]
>>> print type(u)[/color][/color][/color]
<type 'array'>[color=blue][color=green][color=darkred]
>>> print u.shape[/color][/color][/color]
(16, 17, 21)[color=blue][color=green][color=darkred]
>>> u[0,0,0][/color][/color][/color]
-1728
Is there something wrong in using slices like this in objects? A
better way?
Comment