[Python] Are there any drawbacks to assigning `__slots__` from `__annotations__` in a Python...

Discussão em 'Python' iniciado por Stack, Dezembro 19, 2024.

  1. Stack

    Stack Membro Participativo

    With Python's type annotations, classes that declare __slots__ often end up duplicating the class identifiers.

    A brief example:

    class Event:
    location: tuple[float, float]
    index: int

    __slots__ = (
    "location",
    "index",
    )


    An alternative to this is to use annotations to declare slots.

    class Event:
    location: tuple[float, float]
    index: int

    __slots__ = tuple(__annotations__)


    Is there any reasons to avoid deriving __slots__ from __annotations__ in Python 3.10 & newer?

    Continue reading...

Compartilhe esta Página