From https://death.andgravity.com/namedtuples linked to above by Titus:
pairs of things, like HTTP headers (a dict is not always appropriate, since the same header can appear more than once, and the order does matter in some cases)
To me, this is a perfect case of behavior that namedtuples are suited
towards. It shows
that namedtuples are still necessary despite the presence of dataclasses.
The maintainability
problems of namedtuples came from people using them where the order
actually wasn't
important.
I wouldn't be opposed to adding this to the documentation for namedtuple::
.. note::
Also consider whether frozen :mod:`dataclasses` suits your use case.
It may be a better
tool to use if:
* the members of your collection are not inherently ordered
* you would like to apply type annotations
Feel free to chime in if you can think of other reasons to use a dataclass
instead!
I definitely feel that the backwards compatibility implications of
deprecating it are huge.
Meanwhile, the presence of dataclasses means that there are far fewer use
cases where
namedtuples are the right tool. Let's document that!
On Wed, Jul 28, 2021 at 7:41 PM Thomas Grainger
I've thing I still use NamedTuple for is when I want type safe heterogeneous iterable unpacking, which is only possible for tuples (and NamedTuple) eg I'd like to be able to express both:
tx, rx = trio.MemoryChanel[int]()
And:
with trio.MemoryChannel[int]() as channel: n.start_soon(worker, channel.recieve_channel.clone())
On Thu, 29 Jul 2021, 00:36 Chris Angelico,
wrote: On Thu, Jul 29, 2021 at 9:28 AM Paul Bryan
wrote: I'm with you; since dataclasses were introduced, namedtuple has not see
any use from me, though none of my uses have demanded ultra-high efficiency either.
I wonder how many users are currently relying on namedtuple __getitem__
semantics though. that's functionality dataclasses do not (currently) have.
Random thought I don't know the answer to: Any reason __slots__ can't
be used on a dataclass to improve efficiency?
It can. It still doesn't get efficiency down to where a namedtuple is, and it also doesn't make it into a tuple.
Allow me to simplify matters. You could, instead of using a regular tuple, just use a dict with keys that are consecutive integers. This would behave in a very similar way, and then you could subclass the dict to change the iteration behaviour (or even rely on old-style fallback iteration). Does that mean that tuples are nothing but special-purpose dicts for efficiency? Not even slightly. They are different data structures for different purposes.
It's the same with namedtuple, dataclass, and all the other different tools we have. They serve very different purposes. And even if one of them could truly be described as a higher-performance less-flexible version of another, there'd still be very little benefit to deprecating it. Remember that breaking people's code is a VERY serious concern.
ChrisA _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/TWWK23... Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/BNJ4JI... Code of Conduct: http://python.org/psf/codeofconduct/