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

noreply@sourceforge.net noreply@sourceforge.net
Tue, 23 Oct 2001 07:05:03 -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: None
Group: None
Status: Open
Resolution: Invalid
Priority: 5
Submitted By: Walter Dörwald (doerwalter)
Assigned to: Nobody/Anonymous (nobody)
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: 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