Skip to content

no-slots-in-tuple-subclass (SLOT001)#

Derived from the flake8-slots linter.

What it does#

Checks for subclasses of tuple that lack a __slots__ definition.

Why is this bad?#

In Python, the __slots__ attribute allows you to explicitly define the attributes (instance variables) that a class can have. By default, Python uses a dictionary to store an object's attributes, which incurs some memory overhead. However, when __slots__ is defined, Python uses a more compact internal structure to store the object's attributes, resulting in memory savings.

Subclasses of tuple inherit all the attributes and methods of the built-in tuple class. Since tuples are typically immutable, they don't require additional attributes beyond what the tuple class provides. Defining __slots__ for subclasses of tuple prevents the creation of a dictionary for each instance, reducing memory consumption.

Example#

class Foo(tuple):
    pass

Use instead:

class Foo(tuple):
    __slots__ = ()

References#