DEV Community

Discussion on: Namedtuple in Python

Collapse
 
orenovadia profile image
orenovadia

attrs (predecessor of the builtin dataclasses module), is a library that helps create data transfer objects. It has a section of why you should prefer it over namedtuples. I think it is well worth reading.

(However, they do not mention that named tuples are much faster to instantiate than attrs classes, because they run no validations and are implemented in C)

Collapse
 
rkshrksh profile image
Rakesh Gautam

Yes. As mentioned on attrs docs, Namedtuples have some limitations over attrs. But for the simple use, where you just have to give names to your fields and want a faster solution, I think Namedtuples are the way to go.

Namedtuples are ultimately just Tuples, they are not supposed to be used as some replacement for classes.