4 ms·
> Especially with `slots=True`, dataclasses can take less resources. `slots=True` doesn't exist, though there's an experimental implementation at https://githu
by duckerude 7y ago
> Especially with `slots=True`, dataclasses can take less resources.
`slots=True` doesn't exist, though there's an experimental implementation at https://github.com/ericvsmith/dataclasses/blob/master/dataclass_tools.py https://github.com/ericvsmith/dataclasses/blob/master/datacl... .
The main issue is that slots have to exist at class definition, and @dataclass takes an already defined class, so you'd have to create a clone of the class, which is nasty.
On top of that you can't always add __slots__ manually, because it conflicts with default field values (and field()s). __slots__ wants to add a descriptor as a class attribute but it can't do that if the default value is already an attribute.
- calpaterson 7y agoThe GP is probably referring to the attrs library, which is another (older) way of creating dataclasses than the stdlib way. It has an easy way to create slotted dataclasses. http://www.attrs.org/en/stable/examples.html#slots http://www.attrs.org/en/stable/examples.html#slots