Costs: 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…
there are newer (since 2020) syntactic constructs that might be more to your liking. take a look at the docs again. Incidentally, I'd recommend against Named Tuples for non-trivial software. Because they can be indexed by integer and unpacked like tuples, additions of new fields are backwards-incompatible with existing code.
Attrs – the Python library everyone needs (2016)
61–70 of 115 posts
Re: Attrs – the Python library everyone needs (2016)
#62Using @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.htmlRe: Attrs – the Python library everyone needs (2016)
#63This 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
Re: Attrs – the Python library everyone needs (2016)
#64...or one could just use Python without classes, just functions. TBH I never quite understood why Python has the class keyword, it's a much better language without.
Re: Attrs – the Python library everyone needs (2016)
#65Earlier quoted context omitted.
Pydantic is dataclasses, except types are validated at runtime? It's nice and looks just like a normal dataclass looking at https://pydantic-docs.helpmanual.io/ For any larger program, pervasive type annotations and "compile" time checking with mypy is a really good idea though, which somewhat lessens the need for runtime checking.
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
Re: Attrs – the Python library everyone needs (2016)
#66You can decompose classes that become too big for their own good. You can design your software, layer abstractions intelligently etc. so that having to do such refactoring isn't a big issue.
Python is a language that demands an above average level of discipline compared to many other programming languages I have used, but only because it IMO leans strongly towards empowering the developer instead of restricting them.
Re: Attrs – the Python library everyone needs (2016)
#67IMO 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
Pydantic is dataclasses, except types are validated at runtime? It's nice and looks just like a normal dataclass looking at https://pydantic-docs.helpmanual.io/ For any larger program, pervasive type annotations and "compile" time checking with mypy is a really good idea though, which somewhat lessens the need for runtime checking.
Re: Attrs – the Python library everyone needs (2016)
#68The @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/
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)
#69I think the author is overselling this library. A lot of the problems they mention can be avoided with a consistent application of discipline. You can decompose classes that become too big for their own good. You can design your software, layer abstractions intelligently etc. so that having to do such refactoring isn't a big issue. Python is a language that demands an above average level of discipline compared to man…
Re: Attrs – the Python library everyone needs (2016)
#70Earlier quoted context omitted.
Pydantic is dataclasses, except types are validated at runtime? It's nice and looks just like a normal dataclass looking at https://pydantic-docs.helpmanual.io/ For any larger program, pervasive type annotations and "compile" time checking with mypy is a really good idea though, which somewhat lessens the need for runtime checking.
Pydantic works with mypy, so you have validation at build-time and parsing at runtime.