You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2014-05-29.02:29:08.413>created_at=<Date2014-05-29.02:18:10.673>labels= ['invalid', 'type-bug', 'library']
title='Is __getitem__ and __len__ implementations enough to make a user-defined class sliceable?'updated_at=<Date2014-05-29.02:29:08.412>user='https://bugs.python.org/santosowijaya'
The reference doc for Python data model says that __getslice__ is deprecated [1], and that __getitem__ should be used instead:
"""
Deprecated since version 2.0: Support slice objects as parameters to the __getitem__() method. (However, built-in types in CPython currently still implement __getslice__(). Therefore, you have to override it in derived classes when implementing slicing.)
"""
But I'm getting the following behavior when I try it myself. Is there something I'm missing?
$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> classtup(object):
... def__getitem__(self, i):
... if i ==0: return0
... if i ==1: return1
...
KeyboardInterrupt
>>> classtup(object):
... def__getitem__(self, i):
... if i in (0, 1): return i
... else: raiseIndexError()
... def__len__(self):
... return2
...
>>> t = tup()
>>> len(t)
2
>>> t[0], t[1]
(0, 1)
>>> t[:2]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in __getitem__
IndexError
>>> t[:1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in __getitem__
IndexError
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: