[Python-bugs-list] [ python-Bugs-456876 ] __getitem__ issues

noreply@sourceforge.net noreply@sourceforge.net
Thu, 30 Aug 2001 07:57:44 -0700


Bugs item #456876, was opened at 2001-08-30 07:35
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=456876&group_id=5470

Category: Python Interpreter Core
Group: None
Status: Closed
Resolution: Fixed
Priority: 5
Submitted By: Tim Hochberg (tim-hochberg)
Assigned to: Nobody/Anonymous (nobody)
Summary: __getitem__ issues

Initial Comment:
(Python 2.1, Windows 2000)

__getitem__ is too consistent with __getslice__ and 
not consistant enough with itself.

When getitem called in cases that would trigger 
getslice if it were present, the slice object is 
filled in a way that is consistent with getslice, but 
not consistent with the rest of getitem's behaviour. 
It's also not as useful as it could be.

Here's what getitem receives for various cases:

>>> a[0:1]
slice(0, 1, None)
>>> a[:1] # One of these things is not like the other
slice(0, 1, None)
>>> a[:1:1]
slice(None, 1, 1)
>>> a[::]
slice(None, None, None)
>>> a[:1:]
slice(None, 1, None)

In addition to being inconsistent, this behaviour is 
less useful. Someone on the Numeric's list recently 
ran into this as a stumbling block to subclassing 
UserArray as a way to get arrays that are indexed 
starting with one. (For easier translation of some 
algorithms).

I see that this could potentially break some code, but 
at this point most code that could be broken will 
still be using getslice. Putting off fixing this will 
only make it harder to fix as more people migrate to 
the getitem protocol. 

----------------------------------------------------------------------

>Comment By: Tim Hochberg (tim-hochberg)
Date: 2001-08-30 07:57

Message:
Logged In: YES 
user_id=294744

Great!
If it works for new style classes, that should be fine. 
mumble mumble time machine mumble.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-08-30 07:53

Message:
Logged In: YES 
user_id=6380

In Python 2.2a2, you can already get the right behavior
using
new-style classes; I'm not sure it's worth fixing for
classic classes.

Python 2.2a2 (#176, Aug 22 2001, 16:23:28) 
[GCC 2.95.3 19991030 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>> class C(object):
 def __getitem__(s,a): return a

>>> c = C()
>>> c[:]
slice(None, None, None)
>>> c[1:]
slice(1, None, None)
>>> c[:1]
slice(None, 1, None)
>>> c[-1:]
slice(-1, None, None)
>>> c[:-1]
slice(None, -1, None)
>>> 


----------------------------------------------------------------------

Comment By: Tim Hochberg (tim-hochberg)
Date: 2001-08-30 07:38

Message:
Logged In: YES 
user_id=294744

It's worse than I thought: negative indices are even worse:

>>> a[-3:-2:1]
slice(-3, -2, 1)
>>> a[-3:-2]
  File "C:\Documents and 
Settings\tim\Desktop\boa\boa\ExternalLib\PythonInterpreter.p
y", line 65, in push
  File "<console>", line 1, in ?
exceptions.AttributeError : bar instance has no 
attribute '__len__'


----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=456876&group_id=5470