Re:Attrs – the Python library everyone needs (2016)
by ttwwmm on Friday December 24, 2021 @08:40PM
(#29679514)
attrs 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). So it's not obsolete, particularly for anyone already relying on those features.
Re:Attrs – the Python library everyone needs (2016)
by nurettin on Friday December 24, 2021 @11:21PM
(#29680363)
dataclasses 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.
Re:Attrs – the Python library everyone needs (2016)
by ericvsmith on Saturday December 25, 2021 @02:58AM
(#29681371)
dataclasses author here.
Does the keyword-only feature in 3.10 help you at all?
Re:Attrs – the Python library everyone needs (2016)
by abdusco on Saturday December 25, 2021 @03:35AM
(#29681516)
Wow, 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-fi...
Re:Attrs – the Python library everyone needs (2016)
by muxator on Saturday December 25, 2021 @04:13AM
(#29681695)
And a big thank you for the dataclasses module, and for making the life of the whole python community a little easier!
Re:Attrs – the Python library everyone needs (2016)
by nurettin on Monday December 27, 2021 @04:32PM
(#29707008)
Love it, thank you!
Re:Attrs – the Python library everyone needs (2016)
by joshuamorton on Friday December 24, 2021 @08:50PM
(#29679568)
Echoing 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".
Re:Attrs – the Python library everyone needs (2016)
by dreamofkoholint on Friday December 24, 2021 @09:34PM
(#29679818)
Is 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
Re:Attrs – the Python library everyone needs (2016)
by joshuamorton on Friday December 24, 2021 @09:37PM
(#29679836)
Not 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.
Re:Attrs – the Python library everyone needs (2016)
by kissgyorgy on Saturday December 25, 2021 @01:37AM
(#29681026)
attrs 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.
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.