GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Clarification in the `__slots__` documentation · Issue #100315 · python/cpython

Clarification in the __slots__ documentation #100315

New issue
Copy link
New issue
Copy link
Closed
Closed
Clarification in the __slots__ documentation#100315
Copy link
Labels
docsDocumentation in the Doc dirDocumentation in the Doc dir

Description

@kwsp
kwsp
opened on Dec 17, 2022
Issue body actions

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

  • gh-100315: clarification to __slots__ docs. #102621
  • [3.11] gh-100315: clarification to __slots__ docs. (GH-102621) #102687
  • [3.10] gh-100315: clarification to __slots__ docs. (GH-102621) #102688

Activity

  1. kwsp
    added
    docsDocumentation in the Doc dirDocumentation in the Doc dir
    on Dec 17, 2022
  2. AlexWaygood commented on Dec 17, 2022

    @AlexWaygood
    AlexWaygood
    on Dec 17, 2022
    Member
    More actions

    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.

  3. kumaraditya303 commented on Dec 17, 2022

    @kumaraditya303
    kumaraditya303
    on Dec 17, 2022
    Contributor
    More actions

    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.

  4. AlexWaygood commented on Dec 17, 2022

    @AlexWaygood
    AlexWaygood
    on Dec 17, 2022
    Member
    More actions

    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.

  5. AlexWaygood commented on Dec 17, 2022

    @AlexWaygood
    AlexWaygood
    on Dec 17, 2022
    Member
    More actions

    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).

  6. kumaraditya303 commented on Dec 17, 2022

    @kumaraditya303
    kumaraditya303
    on Dec 17, 2022
    Contributor
    More actions

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

    Reacted by Alex Waygood
  7. kwsp commented on Mar 12, 2023

    @kwsp
    kwsp
    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?

  8. kumaraditya303 commented on Mar 12, 2023

    @kumaraditya303
    kumaraditya303
    on Mar 12, 2023
    Contributor
    More actions

    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.

    Reacted by Taylor
  9. kwsp
    added a commit that references this issue on Mar 12, 2023

    pythongh-100315: clarification to `__slots__` docs.

    6f6ba27
  10. kwsp commented on Mar 12, 2023

    @kwsp
    kwsp
    on Mar 12, 2023
    ContributorAuthor
    More actions

    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.

  11. AlexWaygood
    added a commit that references this issue on Mar 14, 2023

    gh-100315: clarification to `__slots__` docs. (#102621)

    88c262c
  12. miss-islington
    added 2 commits that reference this issue on Mar 14, 2023

    pythongh-100315: clarification to `__slots__` docs. (pythonGH-102621)

    855a80f

    pythongh-100315: clarification to `__slots__` docs. (pythonGH-102621)

    c2d6fa1
  13. kwsp
    closed this as completedon Mar 14, 2023
  14. miss-islington
    added 2 commits that reference this issue on Mar 14, 2023

    gh-100315: clarification to `__slots__` docs. (GH-102621)

    2c015a6

    gh-100315: clarification to `__slots__` docs. (GH-102621)

    1f7eb8f
  15. AlexWaygood commented on Mar 14, 2023

    @AlexWaygood
    AlexWaygood
    on Mar 14, 2023
    Member
    More actions

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

  16. carljm
    added a commit that references this issue on Mar 14, 2023

    Merge branch 'main' into inlinecomp2

    6c5f269
  17. Fidget-Spinner
    added a commit that references this issue on Mar 27, 2023

    pythongh-100315: clarification to `__slots__` docs. (python#102621)

    9c61718
  18. warsaw
    added a commit that references this issue on Apr 11, 2023

    pythongh-100315: clarification to `__slots__` docs. (python#102621)

    37736fc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dirDocumentation in the Doc dir

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.