A Tip A Day – Python Tip #10: 2 reasons to use Python Type Hints

2 reasons to use Python Type Hints

Type hints are used to mention the expected data type of the variable.

Even before Python 3.6, developers provide preferred data type in comments. So that the same variable may not be used with different datatype in further enhancements.

After Python 3.6, Type hints were introduced.

Why we need to use Type Hints?

Reason #1: Type hints can be used mention data type of a variable.

Syntax:

<variable_name>: <expected data type>

Example:

amount: int = 1000

We can also declare a variable using type hint alone without even defining the values.

Example:

total_amount: int
sales_1 = 100
sales_2 = 200
total_amount = sales_1 + sales_2

Type hints will never force the data type of the variable. Type hint is only for user reference.

So that, the usage of the same variable in later versions, which is going to be handled by different developers will get an idea about the expected value of the variable.

This will reduce runtime errors like, “Type Error” when string values are used with “*” operator for multiplication.

It is just a hint and when an integer hinted variable is assigned with string value, no error will be raised at runtime to enforce the data type.

Reason #2: Used in functions for Parameters and return types.

Type Hints are also very useful in functions.
These helps the developers to know the expected data type of the parameters and available data type of the return value.
For example, in the below example, the parameters must be a number.

# Both the functions expect only numbers and not string values
def multiply(a, b):
    return a * b

def addition(a, b):
    return a + b

print(addition(5,6))
print(addition("5","6"))

11
56

print(multiply(5,6))
print(multiply("5","6"))
30
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-051e65134284> in <module>
      1 print(multiply(5,6))
----> 2 print(multiply("5","6"))

<ipython-input-7-9af0c4484b3c> in multiply(a, b)
      1 def multiply(a, b):
----> 2     return a * b

TypeError: can't multiply sequence by non-int of type 'str'

In the above example, the expected datatype of the parameters are integers.
But string values were passed.
Now we will set the type hints for parameters.

def addition(a: int, b: int) -> int:
    return a + b

def multiply(a: int, b: int) -> int:
    return a * b

print(addition(5,6))
print(addition("5","6"))

print(multiply(5,6))
print(multiply("5","6"))
11
56
30 

---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
  in 
       1 print(multiply(5,6))
 ----> 2 print(multiply("5","6"))
  in multiply(a, b)
       1 def multiply(a: int, b: int):
 ----> 2     return a * b
       3 
       4 def addition(a: int, b: int):
       5     return a + b
 TypeError: can't multiply sequence by non-int of type 'str'

As you can see, there is no change in the result.
Type hints will only help the developer by giving an extra information about the expected type of the variable. Here, it is said to send only the integer values.
It will not force the compiler to only allow the specified data type.
Still the Dynamic Typing of Python will work.

 I hope you enjoyed learning Type Hints.

Lets explore more about Python in future tips.

More interesting Python tips:

A Tip A Day – Python Tip #9: 3 Interesting things about format {}

A Tip A Day – Python Tip #6 – Pandas Merge

A Tip A Day – Python Tip #5 – Pandas Concat & Append

All Other Tips

Other interesting concepts:

SpaCy Vs NLTK – Basic NLP Operations code and result comparison

Best way to Impute NAN within Groups — Mean & Mode

Default image
Asha Ponraj

Data science and Machine Learning enthusiast | Software Developer | Blog Writter

Articles: 84

Leave a Reply