Is it a good practice to use __annotations__.keys() for __slots__?

Viewed 76

Recently I learned about __slots__, and started using them on my codes.
And I came up with this idea of using annotations for slots.

class Foo:
    bar: int
    baz: str
    __slots__ = __annotations__.keys()

This allows me to change attributes without caring about __slots__.
But none of the codes I've seen so far uses this technique,
and just uses straightfoward __slots__ = ('attr1', 'attr2').

So I began wondering if there is a flaw/drawback to this useful trick.
Perhaps the type of __slots__ affect performance? Everyone seems to be using tuple of str.

0 Answers
Related