4

The whole point of __slots__ is to save space.

But I saw people add __dict__ to it so that new attributes can be added. Isn't this defeating the goal of __slots__?

user11869
  • 1,083
  • 2
  • 14
  • 29
  • Just because you _can_ add `__dict__` to `__slots__` doesn't mean you should, or that there is any good reason to. – agf Sep 28 '11 at 15:11
  • `__Slots__` and `__dict__` are different. You have got the basic concept right. This post might help you:
    http://stackoverflow.com/questions/472000/python-slots Hope that helps :)
    – varunl Sep 28 '11 at 15:14
  • `__slots__` also creates descriptors that make attribute access faster. Having `__dict__` in `__slots__` will still create a dictionary and allow dynamic attributes, but accessing them will be (very) slightly faster. (Sorry for poking an old thread, I felt like the accepted answer was insufficient) – Exa Jul 10 '20 at 02:18

1 Answers1

4

Yes. If you want __dict__, then just don't use __slots__.

Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224