Consider using the dataclass
here instead for simplicity, performance gains, and consistency
The NamedTuple
has some drawbacks such as Python allows accidentally compare NamedTuple
to a tuple or namedtuple with the same number of fields:
That is probably unexpected behaviour, and can result in bugs. Additionally, NamedTuple
are less flexible and more brittle than dataclass
because:
NamedTuple
.NamedTuple
values.__init__
, __repr__
, etc.These drawbacks can be avoided by using a dataclass
.
If our GitHub code review bot spots this issue in your pull request it gives this advice:
1 | + | class FooBarClass(NamedTuple): |
Consider using the dataclass
here instead for simplicity, performance gains, and consistency
- | class FooBarClass(NamedTuple): |
+ | @dataclass(frozen=True) |
+ | class FooBarClass: |
pass |
2 | + | pass |