Live data from Hacker News

Attrs – the Python library everyone needs (2016)

glyph.twistedmatrix.com

61–70 of 115 posts

Re: Attrs – the Python library everyone needs (2016)

#61
post #22

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.

I don't like newfangled syntactic constructs since they hide what is going on. :) The example in the article was a Point3D class and for that namedtuple is a good choice (points should be immutable). It's unlikely that you'd want to add fields without also making other backwards-incompatible changes.

Re: Attrs – the Python library everyone needs (2016)

#62
The @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/

Re: Attrs – the Python library everyone needs (2016)

#63

This 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

You should look at the older implementation. The whole class itself was made by interpolating a huge string and the exec-ing it but was changed with the ability to dynamically generate classes with type(). Nothing other than motivation and a good proposal is standing in the way of a def() that operates similarly.

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.

This is an obvious troll post but python is a pure OO language, everything is a an object with an associated class. Functions are just instances of FunctionType.

Re: Attrs – the Python library everyone needs (2016)

#65
post #9
post #8

Earlier 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

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

Re: Attrs – the Python library everyone needs (2016)

#66
I 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 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)

#67
post #8
post #4

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

[deleted]

Re: Attrs – the Python library everyone needs (2016)

#68

The @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)

#69

I 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…

Agreed. There was some drama with twisted years ago involving python 2-3 interoperability iirc. The title reads like clickbait and having to hook me on an article this way only serves to repulse me physically.

Re: Attrs – the Python library everyone needs (2016)

#70
post #8

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

Last time I checked. Constrained types do not work with mypy out of the box.

https://github.com/samuelcolvin/pydantic/issues/975

Post reply on HN