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

Clarification in the __slots__ documentation #100315

Closed
kwsp opened this issue Dec 17, 2022 · 9 comments
Closed

Clarification in the __slots__ documentation #100315

kwsp opened this issue Dec 17, 2022 · 9 comments
Labels
docs Documentation in the Doc dir

Comments

@kwsp
Copy link
Contributor

kwsp commented Dec 17, 2022

Documentation

https://docs.python.org/3/reference/datamodel.html#notes-on-using-slots

One of the bulletpoints:

  • Nonempty slots does not work for classes derived from “variable-length” built-in types such as int, bytes and tuple.

Points to clarify:

  • What does "does not work" mean?
  • Whats the set of "variable-length" built-in types? Is str one of them?

Linked PRs

@kwsp kwsp added the docs Documentation in the Doc dir label Dec 17, 2022
@AlexWaygood
Copy link
Member

AlexWaygood commented 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
Copy link
Contributor

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
Copy link
Member

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
Copy link
Member

AlexWaygood commented 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
Copy link
Contributor

Yeah, that's the best place I can find too.

@kwsp
Copy link
Contributor Author

kwsp commented 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
Copy link
Contributor

kumaraditya303 commented 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 added a commit to kwsp/cpython that referenced this issue Mar 12, 2023
refer to tp_itemsize in discussion on "variable-length" built-in types
@kwsp
Copy link
Contributor Author

kwsp commented 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.

AlexWaygood pushed a commit that referenced this issue Mar 14, 2023
refer to tp_itemsize in discussion on "variable-length" built-in types
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 14, 2023
refer to tp_itemsize in discussion on "variable-length" built-in types
(cherry picked from commit 88c262c)

Co-authored-by: T <tnie@tuta.io>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 14, 2023
refer to tp_itemsize in discussion on "variable-length" built-in types
(cherry picked from commit 88c262c)

Co-authored-by: T <tnie@tuta.io>
@kwsp kwsp closed this as completed Mar 14, 2023
miss-islington added a commit that referenced this issue Mar 14, 2023
refer to tp_itemsize in discussion on "variable-length" built-in types
(cherry picked from commit 88c262c)

Co-authored-by: T <tnie@tuta.io>
miss-islington added a commit that referenced this issue Mar 14, 2023
refer to tp_itemsize in discussion on "variable-length" built-in types
(cherry picked from commit 88c262c)

Co-authored-by: T <tnie@tuta.io>
@AlexWaygood
Copy link
Member

AlexWaygood commented Mar 14, 2023

Thanks for the PR @kwsp, and thanks for helping us figure this out @kumaraditya303! Fixed on 3.10-3.12.

carljm added a commit to carljm/cpython that referenced this issue Mar 14, 2023
* main: (50 commits)
  pythongh-102674: Remove _specialization_stats from Lib/opcode.py (python#102685)
  pythongh-102660: Handle m_copy Specially for the sys and builtins Modules (pythongh-102661)
  pythongh-102354: change python3 to python in docs examples (python#102696)
  pythongh-81057: Add a CI Check for New Unsupported C Global Variables (pythongh-102506)
  pythonGH-94851: check unicode consistency of static strings in debug mode (python#102684)
  pythongh-100315: clarification to `__slots__` docs. (python#102621)
  pythonGH-100227: cleanup initialization of global interned dict (python#102682)
  doc: Remove a duplicate 'versionchanged' in library/asyncio-task (pythongh-102677)
  pythongh-102013: Add PyUnstable_GC_VisitObjects (python#102014)
  pythonGH-102670: Use sumprod() to simplify, speed up, and improve accuracy of statistics functions (pythonGH-102649)
  pythongh-102627: Replace address pointing toward malicious web page (python#102630)
  pythongh-98831: Use DECREF_INPUTS() more (python#102409)
  pythongh-101659: Avoid Allocation for Shared Exceptions in the _xxsubinterpreters Module (pythongh-102659)
  pythongh-101524: Fix the ChannelID tp_name (pythongh-102655)
  pythongh-102069: Fix `__weakref__` descriptor generation for custom dataclasses (python#102075)
  pythongh-98169 dataclasses.astuple support DefaultDict (python#98170)
  pythongh-102650: Remove duplicate include directives from multiple source files (python#102651)
  pythonGH-100987: Don't cache references to the names and consts array in `_PyEval_EvalFrameDefault`. (python#102640)
  pythongh-87092: refactor assemble() to a number of separate functions, which do not need the compiler struct (python#102562)
  pythongh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives (python#102631)
  ...
Fidget-Spinner pushed a commit to Fidget-Spinner/cpython that referenced this issue Mar 27, 2023
refer to tp_itemsize in discussion on "variable-length" built-in types
warsaw pushed a commit to warsaw/cpython that referenced this issue Apr 11, 2023
refer to tp_itemsize in discussion on "variable-length" built-in types
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

No branches or pull requests

3 participants