Hacker News new | ask | show | jobs
by sologoub 2951 days ago
On the __slots__, the article claims that these do: https://realpython.com/python-data-classes/#optimizing-data-...

EDIT: From PEP 557: https://www.python.org/dev/peps/pep-0557/#support-for-automa...

“Support for automatically setting __slots__?

At least for the initial release, __slots__ will not be supported. __slots__ needs to be added at class creation time. The Data Class decorator is called after the class is created, so in order to add __slots__ the decorator would have to create a new class, set __slots__, and return it. Because this behavior is somewhat surprising, the initial version of Data Classes will not support automatically setting __slots__. There are a number of workarounds:

- Manually add __slots__ in the class definition.

- Write a function (which could be used as a decorator) that inspects the class using fields() and creates a new class with __slots__ set.”

1 comments

The article doesn't discuss slots and default values. Here's the actual reference implementation for dataclasses where it's discussed: https://github.com/ericvsmith/dataclasses/issues/28

Your second solution works fine (and is what I'd referenced). For your first solution, I'd again redirect you to my statement "they don’t support __slots__ and default values" and the above link.

It’s not my solution, just a direct quote from PEP.