GeezerVison is showing github.com — Clarification in the `__slots__` documentation · Issue #100315 · python/cpython
Clarification in the __slots__ documentation #100315ActivityAlexWaygood commented on Dec 17, 2022 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. kumaraditya303 commented on 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 commented on Dec 17, 2022
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. AlexWaygood commented on Dec 17, 2022 kumaraditya303 commented on Dec 17, 2022 Yeah, that's the best place I can find too. kwsp commented on Mar 12, 2023 ContributorAuthor More actions @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 commented on Mar 12, 2023
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. kwsp commented on Mar 12, 2023 ContributorAuthor More actions AlexWaygood commented on Mar 14, 2023 Thanks for the PR @kwsp, and thanks for helping us figure this out @kumaraditya303! Fixed on 3.10-3.12.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|