Member-only story

Why Using Tuples Could Be Frowned Upon In Python Production Code

Liu Zuo Lin
3 min readNov 20, 2024

--

Read Free: https://zlliu.medium.com/why-using-tuples-could-be-frowned-upon-in-python-production-code-0e236c600048?sk=643206f9fe7a82bf9498b2da203b14d2

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

--

--

Liu Zuo Lin
Liu Zuo Lin

Written by Liu Zuo Lin

SWE @ Meta | [Ebook] 101 Things I Never Knew About Python: https://payhip.com/b/vywcf

Responses (4)