Python typing hints are actually nothing new when writing code program. Basically, the standard library for typing hints itself has appeared since PEP 484 and can be used in python version 3.5 and above. With the python typing hint, it actually makes it easier for us when defining a value in a method, argument or function. Usually, in python we don’t need to define data types for a value such as using string or integer argument before define a value. However, since the appeared of python typing hints, personally, it helps us to speed up the development process when we need to double check the data types of a value.
When i’m started using these typing hints or annotations, at first it’s very annoying cause i think it slower my process whenever write the code. However, after slow start of using python typing hints, it’s actually very helpful when i encounter a variable that i don’t know if it’s suitable for string or integer and defining a boolean constant like True or False. What’s more, these standard typing hints will be very easy when we work or collaborate with other developers who at least possibly could help the others who read our code, and understand what the purpose of a value that we define it.
As for noted, when we’re going to use these typing hints, the Python runtime will not process or compile the annotation and will only be treaded like a normal comment so it has no effect on code performances.
Typing Hints
Python typing hints can actually be used in two ways, you use the module typing or directly define a value in your method, function or argument. For the easiest way, let’s look at the program code below :
def __init__(self, items: int):
self.items = items
The program code above is very simple, we make a function to initialize the object that we will use later. Assuming, if we don’t define the parameters of the items, surely we will be confused, these items are integer or string? or it might even be set as NoneType
object. It’s pretty obvious because the definition of the items themselves is very universal when the others people are reading our code. However, when we declare that the items set as an integer, i’m pretty sure that the others people will understand the purpose of our basis code, something like how or what to input the items and how the process of using the object items.
For the use of the typing module itself we can see as an example below :
from typing import Iterable
def foo(bar: Iterable[str]):
for bar in bars:
print("Foo" + bar)
In the code above, there is a function with a parameter bar where the parameter is defined as Iterable
taken from the module typing. Iterable
itself is useful for checking and accepting all the values that has been inputted repeatedly without the need to pay the ttention whether the value it’s passed is full of a List
or Tuple
or not. As for concerns, the declaration of Iterable
it can be declared a one or more than data type, we can using only string or joined string and integer. As i gave example earlier, assume that if the function in the program code does not have an Iterable
annotation, the understanding of the others who will read these parameters it can be just a some function to initialize objects with string or integer data types, whereas, what we want the function is to accept all values with any data type that are wrapped in List
or Tuple
form.
Readability changing the game
Like i said before, when we using python typing hints the biggest advantage lies in the code that will be easy to read. Take an example like this, when you are just collaborating on new project with your friend, and you see that your friend’s legacy code is very numerous or complext and there are many undefined functions or values, the problem you facings are start from :
- What are the inputs and outputs of this value
- What is the data type of this value
The appeared of these typing hints at least makes it easy for us to do it, especially if the project does not have good documentation regarding the code. In addition, the use of typing hints is also very influential when we are going to create documentation something like API documentation, and we don’t need to be bothered about the difficulty of writing a description of what data type is used in a value. The rest of the others, i admit that the use of typing hints is very annoying for the first time and seems difficult to understand when using typing modules with more varied data types such as Union, NamedTuple, List, Iterable, Optional or others, but i’m also pretty sure for the next few moments, the use of python typing hints will become very common in python works.