3 ms·
Less fully-featured, sure, but also a bit more cleanly designed (in my opinion). Are there features from attrs that you would miss with dataclasses?
by shoyer 8y ago
Less fully-featured, sure, but also a bit more cleanly designed (in my opinion). Are there features from attrs that you would miss with dataclasses?
- metalliqaz 8y agoThis is spot on. The design of attrs remines me a little bit of the syntax from a declarative ORM, for example. I'm sure it can do very powerful things that I've not had occasion to use, but it is heavy. The @dataclass format is very clean and seems more like the syntactic sugar that I expect from Python. One of the prime uses of a dataclass is to be a mutable namedtuple. And the syntax can be almost identical: Part = make_dataclass('Part', ['part_num', 'description', 'quantity']) (from Raymond Hettinger's twitter) This has the added benefit of not requiring type hinting, if you don't want to bother with such things.
- froztbyte 8y agoin a freshly created venv, with attrs and ipython (and deps, of course): (attrs) % ipython Python 2.7.14 (default, Oct 19 2017, 23:28:49) Type "copyright", "credits" or "license" for more information. IPython 5.6.0 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. In [1]: import attr In [2]: Part = attr.make_class("Part", ["part_num", "description", "quantity"]) In [3]: Part? Init signature: Part(self, part_num, description, quantity) Docstring: <no docstring> Type: type Outside of the name of the thing that creates the item, the syntax is identical to what you have here. edit: removed triple backticks, indented with 2sp according to https://news.ycombinator.com/formatdoc https://news.ycombinator.com/formatdoc rules
- deleted 8y ago[deleted]