Hacker News new | ask | show | jobs
by dfee 2951 days ago
The problem with dataclasses are:

1) they don’t support __slots__ and default values

2) type hints aren’t supported yet, and it doesn’t appear they’ll be added to the 3.6 backport

The former issue will be resolved (maybe in 3.8?) but it will require either a metaclass approach or the @dataclass decorator to build a separate class.

The good news is that dataclasses (the backport) work on PyPy 6.0.0.

1 comments

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.”

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.