Not all classes do have a dict. If the classvariable
__slots__
is used, then only the names from the supplied sequence are available and there is no __dict__
. This is used to save memory. If you have only one instance of the class, you don't care. If you have one million, you care.class Foo: __slots__ = ("x", "y") Foo().__dict__
Output:AttributeError: 'Foo' object has no attribute '__dict__
Dataclasses supports this too:from dataclasses import dataclass @dataclass(slots=True) class Foo2: x: int y: intIf you use a class with slots, you cannot add new attributes to the class.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!