Earlier quoted context omitted.
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.
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.
Attrs – the Python library everyone needs (2016)
81–90 of 115 posts
Re: Attrs – the Python library everyone needs (2016)
#82Earlier quoted context omitted.
Pydantic types will be checked by mypy or any other static type analysis tool as well. I don’t expect any type-related thing to be remotely safe in Python without applying at least mypy and pylint, potentially pyright as well, plus, as always with an interpreted language, unit tests for typing issues that would be caught by a compiler in another language
Welp not sure why I caught downvotes for using the two most common static analysis tools for Python, but here we are on Hacker News
Using mypy gives you the type safety equivalent of a compiled language. If you're using mypy, you don't need any additional validation that you wouldn't use in java or c++. I didn't downvote you, but the needless defense in depth is weird.
Re: Attrs – the Python library everyone needs (2016)
#83Costs: Depending on a relatively unknown library. Using arcane class decorators and unusual syntactic constructs: @attr.s and x = attr.ib() (a pun?). Benefits: Saving at best 10-15 lines of boilerplate per data class. Much less if namedtuple works for you. If you want to save lines in __init__ you can write "for k, v in locals().items(): setattr(self, k, v)". But you shouldn't. Edit: Forgot to add to the most importa…
No more than with namedtuples (in fact, both use essentially the same magic: code generation and `eval`).
Re: Attrs – the Python library everyone needs (2016)
#84But all I can really feel is gratitude for all of us not having to do namedtuple/slot contortions anymore. Good riddance.
Re: Attrs – the Python library everyone needs (2016)
#85The @dataclass decorator [1] as proposed in PEP 557 [2] has also been available since Python 3.7 was released in 2018. Using @dataclass the example from OP would look like: from dataclasses import dataclass @dataclass class Point3D: x: float y: float z: float [1]: https://docs.python.org/3/library/dataclasses.html [2]: https://www.python.org/dev/peps/pep-0557/
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.
Re: Attrs – the Python library everyone needs (2016)
#86IMO Pydantic is way more ergonomic, has great defaults, and easier to bend to your will when you want to use it a little differently. Lots of love to Attrs, which is a great library and is a component of a lot of great software. It was my go-to library for years before Pydantic matured, but I think a lot of people have rightly started to move on to Pydantic, particularly with the popularity of FastAPI
Yes, you can do validation in attrs, but it's not meant to be used the same way as pydantic. For serialization, you need cattrs, which is a completely different package.
Re: Attrs – the Python library everyone needs (2016)
#87Earlier quoted context omitted.
well one appeal is that you dont have to write constructors, that‘s already enough of a win for me. then you get sane eq, and sane str, and already you remove 90% boilerplate
I really, genuinely don't get the appeal. I don't follow the "less code = better" ideology so maybe that's a contributor but I really don't see how this: class Person: def __init__(self, name, age): self.name = name self.age = age is any worse than this: @dataclass class Person: name: str age: int I'm not writing an eq method or a repr method in most cases, so it just doesn't add much for the cost.
Until you need them for debugging.
And dataclasses make them free, at lesst syntactically.
Re: Attrs – the Python library everyone needs (2016)
#88This is only remotely relevant but I recently learned that the related `dataclasses` is implemented by constructing _string representations_ of functions and calling `exec` on them. https://github.com/python/cpython/blob/3.10/Lib/dataclasses.... Kind of blew my mind
Ew. How can there not be some better way to do it?
Re: Attrs – the Python library everyone needs (2016)
#89Earlier quoted context omitted.
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.
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.
Does the keyword-only feature in 3.10 help you at all?
Re: Attrs – the Python library everyone needs (2016)
#90Earlier quoted context omitted.
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.
dataclasses author here. Does the keyword-only feature in 3.10 help you at all?
@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...