Slice inconsistency?

Stephen Horne $$$$$$$$$$$$$$$$$ at $$$$$$$$$$$$$$$$$$$$.co.uk
Sat Sep 27 17:12:28 CEST 2003


On 27 Sep 2003 06:45:23 -0700, roberto at dealmeida.net (Roberto A. F. De
Almeida) wrote:

>Stephen Horne <$$$$$$$$$$$$$$$$$@$$$$$$$$$$$$$$$$$$$$.co.uk> wrote in message news:<bqeanvgel9ra3va44j090bmn2l126t340g at 4ax.com>...
>> 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.
>
>Yes, I believe I was not very clear. This is the inconsistency I was
>talking about. (And this is with Python 2.3.1)

Hmmm.

I basically used your earlier print-the-index-to-getitem code (on
Python 2.3) and got...

"""
>>> a[:-1]
slice(None, -1, None)
"""

but then I realised I did one thing different compared with your
example - I inherited from 'object', creating a new-style class (which
is getting to be a habit). Doing it without the inheritence from
object, I got...

"""
>>> b[:-1]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: y instance has no attribute '__len__'
"""

which is exactly what you got. Which suggests that the attempt to
translate that '-1' to a positive subscript is an old convenience,
kept for backward compatibility in old-style classes, but dropped for
greater generality in new-style classes.

I can understand the assumed-to-be old approach - slicing isn't
implemented at present on any builtin container except those that use
subscripts (it works on lists but not dictionaries, for example) but
in terms of generality I'm rather glad we can opt out of it by using
new style classes. While hashing doesn't support it, some
mapping-style data structures can reasonably support slicing by key -
and as in your example, even when using subscripts (as in your
example) the length may be unknown in advance, and the container may
not even have a simple scalar length.

>Data is only retrieved when you slice a variable like above, and only
>the data sliced is requested from the OPeNDAP server, instead of the
>whole range. If I use multiple slices, eg,

Makes good sense.


-- 
Steve Horne

steve at ninereeds dot fsnet dot co dot uk




More information about the Python-list mailing list