Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is __getitem__ and __len__ implementations enough to make a user-defined class sliceable? #65797

Closed
santosowijaya mannequin opened this issue May 29, 2014 · 2 comments
Closed
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@santosowijaya
Copy link
Mannequin

santosowijaya mannequin commented May 29, 2014

BPO 21598

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 = None
closed_at = <Date 2014-05-29.02:29:08.413>
created_at = <Date 2014-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 = <Date 2014-05-29.02:29:08.412>
user = 'https://bugs.python.org/santosowijaya'

bugs.python.org fields:

activity = <Date 2014-05-29.02:29:08.412>
actor = 'santoso.wijaya'
assignee = 'none'
closed = True
closed_date = <Date 2014-05-29.02:29:08.413>
closer = 'santoso.wijaya'
components = ['Library (Lib)']
creation = <Date 2014-05-29.02:18:10.673>
creator = 'santoso.wijaya'
dependencies = []
files = []
hgrepos = []
issue_num = 21598
keywords = []
message_count = 2.0
messages = ['219326', '219327']
nosy_count = 1.0
nosy_names = ['santoso.wijaya']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue21598'
versions = ['Python 2.7']

@santosowijaya
Copy link
Mannequin Author

santosowijaya mannequin commented May 29, 2014

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.
>>> class tup(object):
...     def __getitem__(self, i):
...             if i == 0: return 0
...             if i == 1: return 1
...             
KeyboardInterrupt
>>> class tup(object):
...     def __getitem__(self, i):
...             if i in (0, 1): return i
...             else: raise IndexError()
...     def __len__(self):
...             return 2
... 
>>> 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

[1] https://docs.python.org/2/reference/datamodel.html#object.\_\_getslice__

@santosowijaya santosowijaya mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels May 29, 2014
@santosowijaya
Copy link
Mannequin Author

santosowijaya mannequin commented May 29, 2014

Hm. The docstring for __getitem__ doesn't mention it can/should be accepting slice object as argument. That's what I'm missing. Doc patch?

@santosowijaya santosowijaya mannequin closed this as completed May 29, 2014
@santosowijaya santosowijaya mannequin added the invalid label May 29, 2014
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

0 participants