top | item 40861317

(no title)

justanotherbody | 1 year ago

It'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

discuss

order

eesmith|1 year ago

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