Why Should You Use typing In Python?

Artem Rys
python4you
Published in
2 min readJan 28, 2020

--

Photo by Nicole Wolf on Unsplash (I just like this photo)

I am using typing at my work project and now trying to use it for my personal or freelance projects only if it is not a one-time script. No sense to invest time in the 15-minute thing. And why you should use it too.

Lots of people like Python because it provides a fast way to build software. But on the other hand, after some time of the team development, it can be hard to understand the code for the team members. Especially, for the new ones.

I do personally believe that it increases the readability of the code. Seems to me that it is even more about self-documenting the code, not about the type checking. However, yes, it helps to find some obvious bugs.

My favorite example is a variable called data. Is it a list? Or maybe a dict? Or maybe something custom? Looks like that you need to invest some time in figuring this out. And probably repeat it one more time in a month or two.

Just look at this (what is going on here?):

def process(data):
cleanup(data)
processed_data = processing.process(data)
save_results_to_db(processed_data)

If you are interested in creating better software, it is really easy to start. Just use typing library. There are lots of usage examples at the provided link.

--

--

Artem Rys
python4you

Principal Software Engineer @ Splunk. Writing about Python, GitHub and Splunk.