[Python-bugs-list] [ python-Bugs-473985 ] str, __getitem__ and slices

noreply@sourceforge.net noreply@sourceforge.net
Tue, 23 Oct 2001 07:50:57 -0700


Bugs item #473985, was opened at 2001-10-23 03:39
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=473985&group_id=5470

>Category: Type/class unification
>Group: Python 2.2
Status: Open
>Resolution: Remind
Priority: 5
Submitted By: Walter Dörwald (doerwalter)
>Assigned to: Guido van Rossum (gvanrossum)
Summary: str, __getitem__ and slices

Initial Comment:
Using slices with __getitem__ doesn't work with 
classes derived from str:
---
class S(str):
   def __getitem__(self, index):
      print "getitem", index
      return str.__getitem__(self, index)
   def __setitem__(self, index, value):
      print "setitem", index, value

s = S("foo")
print s[0]
print s[0:2]
s[0] = "b"
s[0:2] = "bar"
---
This prints:
---
getitem 0
f
fo
setitem 0 b
setitem slice(0, 2, None) bar
---
instead of
---
getitem 0
f
getitem slice(0, 2, None)
fo
setitem 0 b
setitem slice(0, 2, None) bar
---
i.e. when no __getslice__ is defined s[0:2] will not 
be forwarded to __getitem__, but it seems to work for 
__setitem__.

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

>Comment By: Guido van Rossum (gvanrossum)
Date: 2001-10-23 07:50

Message:
Logged In: YES 
user_id=6380

I've thought about this some more.

It should be fixed, but the fix is complex, and think I'd
rather not make this change in 2.2 -- (1) I have little
time, and (2) we've already issued a beta release and a
declared a feature freeze. So I'd like to put this on hold
until I can start working on 2.3. This is what would have to
be done:

- Change all built-in sequence types to get rid of the
sq_slice and sq_ass_slice dispatch functions; instead, add a
mapping extension to these types and add mp_subscript and
mp_ass_subscript dispatch functions that recognize slice
objects.

- We'd probably also have to get rid of the sq_item and
sq_ass_item dispatch functions, to roll them into
mp_subscript and mp_ass_subscript.

- Possibly, the whole "PySequenceMethods" structure should
be deprecated; all the operations there are overloading
either numerical or mapping operations.

Extension types that define a sequence methods structure
should continue to work, but the recommendation would be to
migrate these out.

I wish I could do this in 2.2, but it's really too much work
to start now, *and* it would be a serious incompatibility
between 2.2b1 and 2.2b2.

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

Comment By: Walter Dörwald (doerwalter)
Date: 2001-10-23 07:05

Message:
Logged In: YES 
user_id=89016

Oops, this wasn't meant to be a patch. Should have gone 
into the bugs category.

The problems is that http://python.sourceforge.net/devel-
docs/ref/sequence-methods.html says, that __getslice__ 
is "Deprecated since release 2.0", so the user will want to 
implement __getslice__ via __getitem__.

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

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-10-23 06:48

Message:
Logged In: YES 
user_id=6380

I don't think this is a bug. S inherits __getslice__ from
the str base class, but since str is immutable, it doesn't
define a __setslice__ to inherit for S.

BTW I'm reclassifying this as a bug, not a patch. This will
unfortunately invalidate the old URL you had for this entry
(SF doesn't really like reclassification that much.)

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

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