This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: str, __getitem__ and slices
Type: Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: gvanrossum Nosy List: doerwalter, gvanrossum
Priority: normal Keywords:

Created on 2001-10-23 10:39 by doerwalter, last changed 2022-04-10 16:04 by admin. This issue is now closed.

Messages (10)
msg7128 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2001-10-23 10:39
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__.
msg7129 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2001-10-23 13:48
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.)
msg7130 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2001-10-23 14:05
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__.
msg7131 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2001-10-23 14:50
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.
msg7132 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-03-23 03:45
Logged In: YES 
user_id=6380

Raising the priority slightly and marking as a 2.3 issue.
msg7133 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-06-03 21:01
Logged In: YES 
user_id=6380

See (revived) patch 400998?
msg7134 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-06-11 17:00
Logged In: YES 
user_id=6380

Um, that patch doesn't change the fact that strings have a 
__getslice__ method. Maybe that can be thrown out now 
though? str.__getitem__ now supports slice objects, so 
there's no need to have __getslice__ as well.
msg7135 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-06-13 11:38
Logged In: YES 
user_id=6380

I'm closing this despite the fact that it's not 100% fixed.

Because the base class defines __getslice__, the derived
class will inherit this, so you'll have to override it. But
the base class __getitem__ accepts arbitrary slice objects
now.

(Perhaps it should be possible to explicitly set a method to
None to say "I'm not implementing this"? But that has all
sorts of other unforeseen consequences -- ptobably better
not. In the future, strings may lose their __getslice__
method, but for now I think that would slow things down too
much.)
msg7136 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-06-13 11:49
Logged In: YES 
user_id=6380

Um, I have to reopen this, because I lied.

While "abc"[::] works, str.__getitem__("abc", slice(None))
doesn't. And ditto for tuples and lists. I think the
dispatcher tries the sequence getitem before the mapping
getitem.
msg7137 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-06-13 19:25
Logged In: YES 
user_id=6380

OK, that part is now *really* fixed.
History
Date User Action Args
2022-04-10 16:04:33adminsetgithub: 35385
2001-10-23 10:39:01doerwaltercreate