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

__getslice__ still called #46317

Closed
stefanseefeld mannequin opened this issue Feb 7, 2008 · 4 comments
Closed

__getslice__ still called #46317

stefanseefeld mannequin opened this issue Feb 7, 2008 · 4 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@stefanseefeld
Copy link
Mannequin

stefanseefeld mannequin commented Feb 7, 2008

BPO 2041
Nosy @stefanseefeld, @mdickinson

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 2008-02-07.19:09:56.035>
created_at = <Date 2008-02-07.18:19:20.536>
labels = ['interpreter-core', 'type-bug', 'invalid']
title = '__getslice__ still called'
updated_at = <Date 2008-02-07.19:09:56.034>
user = 'https://github.com/stefanseefeld'

bugs.python.org fields:

activity = <Date 2008-02-07.19:09:56.034>
actor = 'mark.dickinson'
assignee = 'none'
closed = True
closed_date = <Date 2008-02-07.19:09:56.035>
closer = 'mark.dickinson'
components = ['Interpreter Core']
creation = <Date 2008-02-07.18:19:20.536>
creator = 'stefan'
dependencies = []
files = []
hgrepos = []
issue_num = 2041
keywords = []
message_count = 4.0
messages = ['62162', '62166', '62168', '62170']
nosy_count = 2.0
nosy_names = ['stefan', 'mark.dickinson']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue2041'
versions = ['Python 2.5', 'Python 2.3']

@stefanseefeld
Copy link
Mannequin Author

stefanseefeld mannequin commented Feb 7, 2008

The python documentation states that since python 2.0 __getslice__ is
obsoleted by __getitem__. However, testing with python 2.3 as well as
2.5, I find the following surprising behavior:

class Tuple(tuple):

  def __getitem__(self, i): print '__getitem__', i
  def __getslice__(self, i): print '__getslice__', i

t = Tuple()
t[0] # __getitem__ called with type(i) == int
t[0:2] # __getslice__ called with type(i) == slice
t[0:2:1] # __getitem__ called with type(i) == slice

@stefanseefeld stefanseefeld mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Feb 7, 2008
@mdickinson
Copy link
Member

I think the docs do a good job of explaining this; in particular, they
say,
in http://docs.python.org/dev/reference/datamodel.html#special-method-
names:

"However, built-in types in CPython currently still implement
__getslice__()."

and explain that __getslice__ is used only to implement the form a[i:j],
and
falls back to __getitem__ if __getslice__ is not implemented.

Getting rid of __getslice__ for builtin types in Python 2.x is probably
one
of those things that would break backwards compatibility. And leaving
it in
is pretty harmless. To avoid surprises, don't implement __getslice__ in
your
own classes. But note that __getslice__ and friends are gone in Python
3.0.

I'd recommend closing this as invalid, but I'll wait for a second
opinion.

@stefanseefeld
Copy link
Mannequin Author

stefanseefeld mannequin commented Feb 7, 2008

Mark,

thanks for the quick follow-up.
OK, i now understand the situation better. The documentation I had read
originally didn't talk about special-casing built-in objects. (And since
I want to extend a tuple, I do have to override __getslice__ since I
want to make sure the returned object still has the derived type.)

Yes, I believe this issue can be closed as invalid.
(Though I believe the docs could be a bit more clear about this.)

Thanks,
Stefan

--

   ...ich hab' noch einen Koffer in Berlin...

@mdickinson
Copy link
Member

Well, documentation patches are always welcome, I believe :)

If you can point to a particular place in the documentation and suggest
alternative (or extra) wording that might help, post it here and I'll deal
with it.

@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
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant