4 ms·
Sad to not see Attrs mentioned. Dataclasses are cool, I guess, but Attrs is seriously great.
by ctoth 2y ago
Sad to not see Attrs mentioned. Dataclasses are cool, I guess, but Attrs is seriously great.
- justanotherbody 2y agoIt's interesting to me that dataclasses seemed to be a slimmed down attrs but in practice I find it replaces namedtuple not attrs For my use the key is how easily you can add simple conversion of values to attrs. IIRC this was intentionally omitted from dataclasses. For a 1-off using a factory with a dataclass is easy but repeated uses send me back to attrs
- eesmith 2y agoYeah, I think namedtuple's popularity (recommended in this link) was a mistake. It's sole modern use case should be to turn tuples used as an unnamed struct into a named struct while preserving tuple indexing for backward compatibility. Most people don't need, say, a[-3] as an alias for a.field_name. Otherwise, for those who want the standard library, use a dataclass with frozen=True for immutability.
- saboot 2y agoattrs looks interesting, can it be used in workflows for processing GBs of data? I don't see any Cython compatibility or other Python to compiled code options. I typically just get by with numpy recarrays, but I'm old fashioned and looking for something more modern.
- justanotherbody 2y agoAttrs would not replace your record array. IIRC you can supply slots so it might be useful for converting raw data into your numpy types, but I suspect you'd find the overhead in function calls problematic If you want easier than recarray and almost as fast I think you're in pandas-ville