Analytics Vidhya
Published in

Analytics Vidhya

A Tip A Day — Python Tip #10: 2 reasons to use Python Type Hints | Dev Skrol

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:

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

# Both the functions expect only numbers and not string valuesdef multiply(a, b):return a * bdef addition(a, b):return a + bprint(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 + bdef multiply(a: int, b: int) -> int:return a * bprint(addition(5,6))print(addition("5","6"))print(multiply(5,6))print(multiply("5","6"))

Output:

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 enjoying learning about 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

Originally published at https://devskrol.com on September 11, 2021.

--

--

--

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Asha Ponraj

Asha Ponraj

Data Science & Machine Learning Enthusiast | Software Developer | Blogger | https://devskrol.com/ | www.linkedin.com/in/asha-ponraj-a5a76b50

More from Medium

Top 5 Python Functions to code like a pro

Introduction To Strings in Python

Importance of python language