Don't return named tuples in new APIs
The article argues against using named tuples in new APIs unless updating existing tuple-returning APIs, citing complexity and recommending alternatives like dataclasses for better clarity and usability.
Read original articleThe article discusses the use of named tuples in new APIs, arguing against their introduction unless updating an existing API that already returns a tuple. Named tuples can complicate both the API's implementation and its usage, as they require supporting both index-based and attribute-based access, leading to increased testing and potential for user error. The author suggests that named tuples signal complexity, which may not be appropriate if the data structure can be represented simply. Alternatives such as dataclasses, dictionaries, TypedDicts, or SimpleNamespace are recommended for better readability and ergonomics. The emphasis is on prioritizing clarity over brevity in code design, advocating for the use of named tuples only when they enhance an existing tuple return type.
- Named tuples should only be used when updating existing APIs that already return tuples.
- They complicate both implementation and user experience by requiring dual access methods.
- Alternatives like dataclasses, TypedDicts, and SimpleNamespace offer better readability and usability.
- Prioritize clarity and ergonomics in code design over brevity.
- Named tuples may indicate that a data structure is too complex for a simple tuple.
Related
Against Names
The article explores the challenges of naming in computer science, highlighting anonymous identifiers in version control and utility CSS as ways to simplify workflows while balancing named and unnamed elements.
Know your Python container types
The article discusses Python's container types: lists, tuples, named tuples, sets, dictionaries, and dataclasses, highlighting their uses, differences, and recommendations for appropriate applications in programming.
Fighting back against proper noun feature names (2021)
Scott Kubie argues against using proper nouns for product features, stating it increases cognitive load and complicates communication. He advocates for simplicity, suggesting unnamed features enhance user experience and clarity.
Don't let dicts spoil your code
Roman Imankulov critiques Python dictionaries for causing technical debt and complicating maintenance. He advocates for domain models, dataclasses, and Pydantic to enhance clarity and structure in evolving codebases.
TypedDicts are better than you think
TypedDicts in Python 3.8 enhance type annotations for dictionaries, allowing optional fields, improving function signatures, and offering better type safety. Upcoming PEPs will introduce extra and read-only item features.
- Named tuples have quirks, such as unexpected equality behavior, which some commenters find problematic.
- Dataclasses are mutable by default, raising concerns about true immutability compared to named tuples.
- Some commenters argue that tuples are fundamental to Python and should not be dismissed in favor of newer constructs.
- There is a sentiment that Python has become overly complex with multiple ways to structure data, which can be confusing.
- Several commenters advocate for dataclasses as a suitable replacement for both named tuples and TypedDicts in new APIs.
>>> Foo = namedtuple("Foo", ["bar"])
>>> Baz = namedtuple("Baz", ["qux"])
>>> Foo(bar="hello") == Baz(qux="hello")
True
This also happens with the "new-style" namedtuples (typing.NamedTuple).I like the convenience of namedtuples but I agree with the author: there are enough footguns to prefer other approaches.
You can use frozen=true to "simulate" immutability, but that just overwrites the setter with a dummy implementation, something you (or your very clever coworker) can circumvent by using object.__setattr__()
So you neither get the performance benefits nor the invariants of actual immutability.
from typing import NamedTuple
class Point(NamedTuple):
x: int
y: int
z: int
I don't see "don't use namedtuples in APIs" as a useful rule of thumb, to be honest. Ordered and iterable return-types make sense for a lot of APIs. Use them where it makes sense.Tuples are a core concept and fundamental data aggregation tool for Python. However, this post uses a trivial `Point()` class strawman to try to shoot down the idea of using tuples at all. IMO that is fighting the language and every existing API that either accepts tuple inputs or returns tuple outputs. That is a vast ecosystem.
According the glossary a named tuple "any type or class that inherits from tuple and whose indexable elements are also accessible using named attributes." Presumably, no one disputes that having names improves readability. So really this weak post argues against tuples themselves.
Or you can just keep returning namedtuple instead of something else, because then you absolutely can skimp on testing whether what you return does, in fact, satisfies the namedtuple's interface.
there was a point a while back where python added __slots__ to classes to help with this; and in practice these days the largest systems are using numpy if they're in python at all
not sure what modern versions do. but in the olden days, if you were creating lots of small objects, tuples were a low-overhead way to do it
Thanks, I hate it. There’s a lot I like about Python, but this is a major pain point.
NamedTuple, TypedDict, Dataclass, Record ... Remember the Zen of Python? „There should be one-- and preferably only one --obvious way to do it“ - it feels like Python has gone way overboard with ways to structure data.
In Javascript everything is an object, you can structurally type them with Typescript and I don’t feel like I’m missing much.
Point(x,y,z)
Related
Against Names
The article explores the challenges of naming in computer science, highlighting anonymous identifiers in version control and utility CSS as ways to simplify workflows while balancing named and unnamed elements.
Know your Python container types
The article discusses Python's container types: lists, tuples, named tuples, sets, dictionaries, and dataclasses, highlighting their uses, differences, and recommendations for appropriate applications in programming.
Fighting back against proper noun feature names (2021)
Scott Kubie argues against using proper nouns for product features, stating it increases cognitive load and complicates communication. He advocates for simplicity, suggesting unnamed features enhance user experience and clarity.
Don't let dicts spoil your code
Roman Imankulov critiques Python dictionaries for causing technical debt and complicating maintenance. He advocates for domain models, dataclasses, and Pydantic to enhance clarity and structure in evolving codebases.
TypedDicts are better than you think
TypedDicts in Python 3.8 enhance type annotations for dictionaries, allowing optional fields, improving function signatures, and offering better type safety. Upcoming PEPs will introduce extra and read-only item features.