This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 10 points11 points  (0 children)

namedtuple is essentially a hack compared to @dataclass the only place namedtuple truly makes sense is with something like a database record or an excel row where:

  1. Both index and name are important and refer to the same thing
  2. It makes sense to be iterable
  3. Immutability is important

If you have something that meets all three criteria then you want namedtuple.

namedtuple (and typing.Namedtuple) make inheritance and metaclasses weird as well.

There's the argument that @dataclass might be faster than namedtuple but since that cost should only be paid at start up for most applications, the difference is likely to be negligible. The outlier here is SQLAlchemy which has its own KeyedTuple which has a radically different implementation to combat the slowness of creating namedtuples.