K is for keyword arguments - Python A to Z
Code in this blog post was written with versions: Python: 3.14
Python A-Z is a blog series about Python. Each day, I share insights, ideas and examples for different parts of Python development that match with the letter of the day. Blaugust is an annual blogging festival in August where the goal is to write a blog post every day of the month.
In PyCon US 2013, Raymond Hettinger talked about writing beautiful, idiomatic Python. It’s a great talk but for this post, I’ll focus on what he talks about around 30 minute mark.
He shares an example (which dates the talk and example nicely; those were the times)
twitter_search('@obama', False, 20, True)
We spend way more time reading code than we do writing it. While modern code editors and IDEs give you a lot of help (as you can see in the following screenshot from VS Code), it’s still an extra step.
Keyword arguments are a great, small step to improving the readability of your code. Compare the two:
twitter_search('@obama', False, 20, True)
# and
twitter_search('@obama', retweets=False, numtweets=20, popularity=True)
The second one is immediately more readable and the better names for your keywords you write, the better its readability becomes.
If something above resonated with you, let's start a discussion about it! Email me at juhis@hamatti.org and share your thoughts. This year, I want to have more deeper discussions with people from around the world and I'd love if you'd be part of that.