I have no types and I must hint
Why would anyone do this?
So, ever since I started programming and learning Python, tuples were something I had a very Cold War type relationship with.
It was something you used in a world in which lists didnāt exist.
Something you used while you were troubleshooting ssh-ed into a web server running only vi in a screen resolution you only thought was possible in bootloader physical console and really had to make your keystrokes count. (I know some people would even dare to say they like this, but not me).
It was something you used if you had complete disregard for order and structure and everything thatās good and nice in the world.
When I see something like return a, b in a PR, it bypasses all of hard-earned emotional regulation and leaves pure, unadulterated āwhy would you do this to meā in its wake.
I thought I would write through this unrelenting hate, and maybe make amends to tuples (which is both normal to want and possible to achieve), but before that, I just wanted to make sure I get to be funny about it first.
How do you write about something as normal, well-adjusted and customary as data structure tier list? There are couple of perspectives: compare their usage, type of data they process, built-in methods⦠But, alas, my hate springs from maligned returns, so letās start there.
Allow me to get vulnerable for a moment and confess what will be monumental to understand this rantfest: type hinting is life and all that is good in this world. If I donāt type hint my code, I have a feeling I goblined that feature and my colleagues will make my typehintlessness the topic of water cooler convos into eternity. Or at the very least, bring the mistake up every chance they get (side-eye).
Data structure tier-list
Lists
Since lists are just tuples that have a blue collar job and a mortgage to pay, let me return to this after I got to have a field day with tuples.
Tuples
I understand that sometimes, you go for that sweet tuple immutability, and I guess I can live with that. But most of the time when I encounter it in code, itās just accidental, returned with expectation to perform some type of modification of the output.
My main gripe with returning both lists and tuples, though, is that it is super hard to infer what, and how much of what, should they return inside a caller method.
List[Dict[str, Union[str, int, float, bool]]] - are we for real here?
Khm⦠Lists
What she just said, but makes your CPU sweatier.
Sets
Same goes for sets, but I have a feeling they are often used as an auxiliary data structure to lists and never really have the limelight for themselves. Iām sorry sets.
Dictionaries
Yeah, TypedDict, good stuff. If only I didn't have to remind my unfix colleagues to use them. Next.
namedtuples and NamedTuples
I hope youāre frustrated after reading this paragraph title. Yes, life is a losing game. Yeah, you are supposed to push the boulder uphill. I guess NamedTuples are okay, as they support type hinting. Itās almost as if we shouldāve had it from the get go.
Classes
Gotta say, knocked it outta park with this one. No notes.
Dataclasses
I have an unhealthy amount of love for dataclasses. They are the anti-tuples for me. I live for the opportunity to serialize another dataclass.
Conclusion
Dataclasses š„° Iām sorry, what was the question?
Disclaimer: No tuples were harmed in the making of this post.