4 ms·
Because namedtuple is a simpler construct than frozen dataclass therefore it is always preferable in Python. The number of characters and lines to access the m
by ReflectedImage 2y ago
Because namedtuple is a simpler construct than frozen dataclass therefore it is always preferable in Python.
The number of characters and lines to access the members of namedtuple is significantly less than dataclass.
dataclass would have similar issues if st_dev was now defined as a string whereas namedtuple would not.
Whilst there maybe edge cases in using simpler constructs, in scripting languages we accept the edge cases as it pays off in 99.99% of cases to simply ignore them. If something goes wrong, you catch the exception "Ask for forgiveness not for permission" if you want to look up the concept.
- eesmith 2y agoThe Pythonic "simpler" does not mean "fewer characters" otherwise APL or Perl/Raku would be more Pythonic. Namedtuple is not strictly simpler. It implements additional features which would not be in an frozen dataclass, which makes "simpler" a personal bias. I've been using Python continuously since 1998 and well remember the "look before you leap" vs. "ask for forgiveness not for permission" debate from the bygone comp.lang.python forum. And I learned the concept of "easier to ask for forgiveness" from the 60 Minutes interview with Grace Hopper back in the 1980s. That have nothing to do with this issue, which is one of taking on an API burden without due consideration simply because it's less typing.
- ericvsmith 2y agoI think you're saying that it takes fewer characters to define a namedtuple. If you're interested in less typing, There's also dataclasses.make_dataclass: >>> Point = dataclasses.make_dataclass("Point", ["x", "y", "z"]) >>> Point(1, 2, 3) Point(x=1, y=2, z=3)