4 ms·
Why not switch to making __slots__ in classes the default and then making attribute changes to an object during runtime an opt-in? It will require a long grace
by SuaveSteve 4y ago
Why not switch to making __slots__ in classes the default and then making attribute changes to an object during runtime an opt-in? It will require a long grace period but wouldn't it help optimisation efforts immensely?
- yedpodtrzitko 4y agoThat would mean all installed dependencies need to comply with this change as well, which is unlikely to happen in any realistic timeframe.
- gjulianm 4y agoThat's going to require quite a lot of changes, it's a giant breaking change. All classes would need someone to go around finding all the attributes that are created and adding an __slots__ dictionary, to avoid regular attribute initialization in __init__ failing. It's a massive task, and it would completely break backwards compatibility for performance gains that not everybody will need.
- BurningFrog 4y agoWhere can I read about what kind of performance improvements `__slots__` brings?
- WillDaSilva 4y agoThe Python docs themselves is a good place to start: https://docs.python.org/3/reference/datamodel.html#slots https://docs.python.org/3/reference/datamodel.html#slots The Python wiki also has some good info about it: https://wiki.python.org/moin/UsingSlots https://wiki.python.org/moin/UsingSlots
- disconnect3d 4y agoApart from the official docs, this video also explains the low level data layout (in CPython) that slots introduce: https://www.youtube.com/watch?v=Iwf17zsDAnY https://www.youtube.com/watch?v=Iwf17zsDAnY
- anamax 4y agodefault __slots__ breaks a lot of monkey patching. An "easier" change would be to add a class attribute "no__dict__", which says that the __dict__ attribute can't be used, which lets the implementation do whatever it wants. That can be incrementally added to classes. Another option is a "no__getattr__" attribute, which disables gettattr and friends.