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
{{ message }}
Repository navigation
Clarification in the __slots__ documentation #100315
The interactive REPL is always useful for discovering some of the answers to questions like this:
>>> classFoo(int):
... __slots__='bar',
...
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: nonempty __slots__ not supported for subtype of 'int'
>>> classFoo(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.
@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.c which 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.
Activity
AlexWaygood commentedon Dec 17, 2022
The interactive REPL is always useful for discovering some of the answers to questions like this:
For your first question, we could possibly change the wording to
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.
kumaraditya303 commentedon Dec 17, 2022
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.AlexWaygood commentedon Dec 17, 2022
Is this definition given anywhere in the documentation? If so, we could maybe link to it from the
__slots__docs here.AlexWaygood commentedon Dec 17, 2022
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).kumaraditya303 commentedon Dec 17, 2022
Yeah, that's the best place I can find too.
kwsp commentedon Mar 12, 2023
@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?kumaraditya303 commentedon Mar 12, 2023
It is not documented as it is an internal implementation detail.
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.pythongh-100315: clarification to `__slots__` docs.
kwsp commentedon Mar 12, 2023
@AlexWaygood sounds good. I added this link to the docs in a PR.
gh-100315: clarification to `__slots__` docs. (#102621)
pythongh-100315: clarification to `__slots__` docs. (pythonGH-102621)
pythongh-100315: clarification to `__slots__` docs. (pythonGH-102621)
gh-100315: clarification to `__slots__` docs. (GH-102621)
gh-100315: clarification to `__slots__` docs. (GH-102621)
AlexWaygood commentedon Mar 14, 2023
Thanks for the PR @kwsp, and thanks for helping us figure this out @kumaraditya303! Fixed on 3.10-3.12.
Merge branch 'main' into inlinecomp2
pythongh-100315: clarification to `__slots__` docs. (python#102621)
pythongh-100315: clarification to `__slots__` docs. (python#102621)