Live data from Hacker News

Attrs – the Python library everyone needs (2016)

glyph.twistedmatrix.com

71–80 of 115 posts

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

#71

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

The point is that for data-bag style classes, you end up writing a lot more boilerplate than that if you use them across a project. Validators (type or content), nullable vs not, read-only, etc.

The minimal trivial case doesn’t look much different, but if you stacked up 10 data classes with read-only fields vs. bare class implementations with private members plus properties to implement read-only, and you would start to see a bigger lift from attrs, as there would be a bunch of boring duplicated logic.

(Or not - if your usecases are all trivial then of course don’t use the library for more complex usecases. But hopefully you can see why this gets complex in some codebases, and why some would reach for a framework.)

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

#72

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.

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)

#73

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.

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)

#74

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

I thought like you for several years. Then one day, I needed a custom type. You can use dicts (or lists, or namedtuples, I guess?), but it just ends up being cleaner and more idiomatic to define a class for the type, because you can define common methods for them. The article mentions quaternions. If you make a quaternion type (class), you can define addition, multiplication, comparison, etc. for it (methods). If you…

You can represent it as whatever and monkey patch it's __mul__. It's a bit complicated with standard library structures that may be implemented in C, though.

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

#76
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…

> Depending on a relatively unknown library

attrs is not “relatively unknown” as Python libraries go.

> Using arcane class decorators and unusual syntactic constructs: @attr.s and x = attr.ib() (a pun?).

There have been conventional, SFW aliases for the punny ones for...a long time.

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

#77

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.

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

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)

#78

Earlier quoted context omitted.

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

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

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)

#79

Earlier quoted context omitted.

Yes. There needs to be a very good reason for me to pull in a third party library (in this day and age, given supply chain attacks, etc.). I don’t see what Attrs gives me that dataclasses does not.

Python's STL is such that using it is a code smell. It's better to just use the right tool for the job: you are almost guaranteed to need at least one external library/module for any project of even moderate complexity. So bite the bullet, invest in the time/tooling to do packaging correctly, and use the very excellent Python ecosystem (isn't it why you are using Python to begin with?) that you have at your disposal.…

Honestly, only use python because I have to. I don't think it belongs inside a decently sized software product

Others disagree, though, so I must use it

What you're saying only bolsters my opinion

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

#80
post #34
post #13

Earlier quoted context omitted.

Named tuples have been around for a long time (since 2.6), whereas dataclasses are a relatively recent addition to the standard library (3.7). Their differences are highlighted in the dataclasses PEP: https://www.python.org/dev/peps/pep-0557/#why-not-just-use-n...

looks like the key thing is immutability

Key thing for me was the ability to add type hinting
Post reply on HN