Repository navigation
Clarification in the __slots__ documentation #100315
Description
Activity
The interactive REPL is always useful for discovering some of the answers to questions like this:
>>> class Foo(int): ... __slots__ = 'bar', ... Traceback (most recent call last): File "<string>", line 1, in <module> TypeError: nonempty __slots__ not supported for subtype of 'int' >>> class Foo(str): ... __slots__ = 'bar', ... >>>
For your first question, we could possibly change the wording to
- TypeError will be raised if nonempty slots are defined for a class derived from a “variable-length” built-in type such as int, bytes or tuple.
For your second question: I don't know what's being referred to by "variable-length builtin types", so I agree that the wording there is a little opaque.
You can check if any type is variable length or not from python with
<type>.__itemsize__ != 0. As for what it means, variable length is a memory layout when data follows the object pointer in single block like tuple in contrast to list which stores items in a separate memory block.As for what it means, variable length is a memory layout when data follows the object pointer in single block like tuple in contrast to list which stores items in a separate memory block.
Is this definition given anywhere in the documentation? If so, we could maybe link to it from the
__slots__docs here.Looks like the best place to link to might be https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_itemsize. Hardly beginner-friendly, but probably better than no link at all (and I think it's good to keep the section on
__slots__in the datamodel terse).Yeah, that's the best place I can find too.
Reacted by Alex Waygood@kumaraditya303 I can't find
__itemsize__documented anywhere in the current documentation(docs search, duckduckgo). Could you please link any docs to__itemsize__? If its not available, could you tell me how you learnt about this, so we can add it to the docs?I can't find itemsize documented anywhere in the current documentation(docs search, duckduckgo).
It is not documented as it is an internal implementation detail.
If its not available, could you tell me how you learnt about this, so we can add it to the docs?
Just read the source code, in this case it is
typeobject.cwhich contains the logic for type objects and memory management but it is not beginner friendly. I know all this because I work in these areas of interpreter often.Reacted by Taylor- added a commit that references this issue
on Mar 12, 2023 Looks like the best place to link to might be https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_itemsize. Hardly beginner-friendly, but probably better than no link at all (and I think it's good to keep the section on slots in the datamodel terse).
@AlexWaygood sounds good. I added this link to the docs in a PR.
- added a commit that references this issue
on Mar 14, 2023 Thanks for the PR @kwsp, and thanks for helping us figure this out @kumaraditya303! Fixed on 3.10-3.12.
- added a commit that references this issue
on Mar 14, 2023
Documentation
https://docs.python.org/3/reference/datamodel.html#notes-on-using-slots
One of the bulletpoints:
Points to clarify:
strone of them?Linked PRs
__slots__docs. #102621__slots__docs. (GH-102621) #102687__slots__docs. (GH-102621) #102688