Python: Dynamic Typing vs Static Typing

Sunil Kumar
4 min readFeb 19, 2022

--

Programing languages can be categorized into two types Dynamic typing and static typing. Python is a dynamically typed language. This means that the Python interpreter does type checking only as the code runs. However, in Statically typed language the checking of the variables/objects

is done at the compile time. So it becomes easy to handle or fix errors if you can find them before running the program.

When you write a program in Dynamic typing language it is not required to define the type of the variables. It is derived based on the values you assign to them. But it becomes harder and harder to maintain the code as it creates confusion and takes a longer time to understand the code.

Some examples of dynamically typed languages are:

  • Python
  • PHP
  • JavaScript

Some examples of statically typed languages are:

  • Java
  • C
  • C++

Example:

Let’s create a variable in python and see how it can change type:

Python:

Java: You can see in the case of java you cannot assign a different type of value other than the variable type.

Python will always remain a dynamically typed language. However, PEP 484 introduced type hints, which make it possible to also do static type checking of Python code.

However, unlike static type language just specifying type hints in python code does not enforce types. We have some tools to perform these checks like mypy.

In the following example, we have two functions one without type hints and one with type hints. When we run the program it just works and does not complain because we are passing string values instead of types int defined for the second function.

But think if the function hello_world_with_type_hint performs division for the parameter passed, In that case, it will through exception.

Wouldn’t it be great if we have some type of checking before executing the code so that we can fix it?

So here come mypy . It will help us to perform the type checking if we have type hints defined in the code.

You can install mypy with pip

Now let’s perform type checking with mypy.

You can see that it show error for the return type and arguments type.

Let’s change the return type to float as advised by mypy.

Now let's call the function with valid arguments type ( int). You can see the success message now.

You saw how mypy helped us to find out the issue before actually running the program. It also recommends the correct options.

But we don’t need to run the mypy for each file of our code one by one. However, we can configure mypy with pre-commit so that it will execute whenever you want to commit the code.

Conclusion:

We saw an example of one of the advantages of adding types to your code: type hints help catch certain errors. It also helps document your code.

If you read so for please consider hitting follow button.

Reference :

  1. https://www.python.org/dev/peps/pep-0484/
  2. https://pypi.org/project/mypy/
  3. https://github.com/pre-commit/mirrors-mypy

--

--

Sunil Kumar
Sunil Kumar

Written by Sunil Kumar

With an extensive professional experience spanning over 16 years in the IT industry, I am a seasoned expert in AWS Cloud, DevOps, FastAPI, and Python.

No responses yet