Member-only story
Why Using Tuples Could Be Frowned Upon In Python Production Code
3 min readNov 20, 2024
Tuples are versatile and useful, but I don’t see them that much in production code. Here are some reasons why:
Writing type hints for tuples might not be the most explicit
Let’s say we have a function that does stuff, and returns a tuple.
def do_stuff() -> tuple[str, int, int]:
"""
some docstring
"""
fruit_name: str = get_fruit_name()
price: int = get_price()
quantity: int = get_quantity()
return fruit_name, price, quantity
^ notice that when we attempt to write our type hint, we use tuple[str, int, int]
This just means that our function is meant to return a tuple of length 3 — the first element being a string, and the other 2 being integers.
Isn’t this pretty straightforward?
The problem with this
- At first glance, we know that
tuple[str, int, int]
is returned - But at first glance, we don’t know what this
tuple[str, int, int]
is supposed to represent