Long story short
PEP-557 introduced data classes into Python standard library, that basically can fill the same role as collections.namedtuple
and typing.NamedTuple
. And now I'm wondering how to separate the use cases in which namedtuple is still a better solution.
Data classes advantages over NamedTuple
Of course, all the credit goes to dataclass
if we need:
- mutable objects
- inheritance support
property
decorators, manageable attributes- generated method definitions out of the box or customizable method definitions
Data classes advantages are briefly explained in the same PEP: Why not just use namedtuple.
Q: In which cases namedtuple is still a better choice?
But how about an opposite question for namedtuples: why not just use dataclass? I guess probably namedtuple is better from the performance standpoint but found no confirmation on that yet.
Example
Let's consider the following situation:
We are going to store pages dimensions in a small container with statically defined fields, type hinting and named access. No further hashing, comparing and so on are needed.
NamedTuple approach:
from typing import NamedTuple
PageDimensions = NamedTuple("PageDimensions", [('width', int), ('height', int)])
DataClass approach:
from dataclasses import dataclass
@dataclass
class PageDimensions:
width: int
height: int
Which solution is preferable and why?
P.S. the question isn't a duplicate of that one in any way, because here I'm asking about the cases in which namedtuple is better, not about the difference (I've checked docs and sources before asking)