$$$$$$$$$$$$$$$$$ at $$$$$$$$$$$$$$$$$$$$.co.uk
Sat Sep 27 09:38:08 CEST 2003
On Fri, 26 Sep 2003 17:28:02 -0400, "Terry Reedy" <tjreedy at udel.edu>
>"Roberto A. F. De Almeida" <roberto at dealmeida.net> wrote in message
>news:10c662fe.0309261245.1f35f43a at posting.google.com...
>> I found that when using negative indices, the slice object passed to
>> __getitem__ depends on the number of slices. An example to clarify:
>> def __getitem__(self, index):
>> return index
>> >>> b = a()
>> >>> print b[:-1]
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in ?
>> AttributeError: a instance has no attribute '__len__'
>> But if I pass a "multiple" slice:
>> >>> print b[:-1,:-1]
>> (slice(None, -1, None), slice(None, -1, None))
>A square-bracketed subscript (index/key) is a *single* object, in this
>case a tuple. The contents of the tuple are irrelevant (for this
>code). Any tuple will be echoed:
I didn't see any sign that he wasn't aware of that.
I would have expected, given that the tuple contains slice objects
constructed from the multiple-slice notation, that the same
translations would be performed on the slices that are inserted into
the tuple that are applied when the single slice is created.
That is, whether the single or multiple notation is used, and whether
the slice objects are placed in a tuple or not, they are constructed
from the tuple notation - the translation from notation to slice
object should be done consistently.
Actually, the translation done in the single case worries me - an
extension module I wrote a while ago should not work if this is the
case (as the slice is based on associative keys, not subscripts, so
the negative subscript trick doesn't apply).
I'd better do some testing, I suppose :-(
>What you probably want is b[:-1][:-1], etc. Each index must be
>separately bracketed to access items in list of lists (etc).
Maybe. Maybe not. I assumed he was doing something similar to what
happens in numeric - they do some rather interesting
slicing-of-multidimensional-container things over there. Of course I'm
not a numeric user, so I may be misunderstanding things.
steve at ninereeds dot fsnet dot co dot uk
More information about the Python-list