7 ms·
As soon as I opened the article I pressed Cmd+F and searched for "dataclasses". This article was correct and addressed a very real need in Python programming—f
by hamstergene 5y ago
As soon as I opened the article I pressed Cmd+F and searched for "dataclasses".
This article was correct and addressed a very real need in Python programming—for year 2016. By now it is obsolete and today's standard library module `dataclasses` does all of that and more.
- ttwwmm 5y agoattrs classes still have several features that dataclasses don't, and likely never will, [like validators and converters](https://www.attrs.org/en/stable/why.html#data-classes https://www.attrs.org/en/stable/why.html#data-classes). So it's not obsolete, particularly for anyone already relying on those features.
- nurettin 5y agodataclasses have methods for iterating fields and inspecting types, so any feature can be added. There is also the benefit of type checking. My grief with dataclasses is how I can't inherit from dataclasses with default fields without making all child class fields also have defaults.
- ericvsmith 5y agodataclasses author here. Does the keyword-only feature in 3.10 help you at all?
- abdusco 5y agoWow, I was unaware of this feature. It appears there are 3 ways to declare fields as keyword-only. @dataclass(kw_only=True) class Birthday: name: str birthday: datetime.date # --- @dataclass class Birthday: name: str birthday: datetime.date = field(kw_only=True) # --- from dataclasses import KW_ONLY @dataclass class Point: x: float y: float _: KW_ONLY z: float = 0.0 t: float = 0.0 https://docs.python.org/3/whatsnew/3.10.html#keyword-only-fields https://docs.python.org/3/whatsnew/3.10.html#keyword-only-fi...
- muxator 5y agoAnd a big thank you for the dataclasses module, and for making the life of the whole python community a little easier!
- nurettin 5y agoLove it, thank you!
- joshuamorton 5y agoEchoing the other commenter, attrs is generally superior to dataclasses (dataclasses is a feature-limited std library "backport" of attrs). It will be updated less often and support less stuff. The only real reason to use dataclasses is if you want to avoid a third-party dependency, which is sometimes valid but doesn't make the more featureful version "obsolete".
- dreamofkoholint 5y agoIs there a reason those features weren't added to data classes? I don't know much about attrs, only professionally coming to python since 3.7, but I'm not going to bring it in if there's something sufficient in the language
- joshuamorton 5y agoNot really, other than they decided to keep data classes very minimal, as standard library comes with maintenance costs (you can't get rid of a feature once you add it), so they kept the strict features, since everyone uses those, and not the complex validation features.
- kissgyorgy 5y agoattrs was first, dataclasses design is actually based on attrs! dataclasses is inferior in features and actually a shame because I see this ignorant attitude everywhere and I have to defend and explain attrs every time we need it.