What is type checking? Why do we need it? What’s the difference between static and runtime type checking?

Python is a strongly typed, dynamic programming language. With it being dynamically typed, types are dynamically inferred, so you can set variable values directly without defining the variable type like in statically typed programming languages such as Java.

name = "Michael"
String name = "Michael";

static vs dynamic programming languages

Strong and dynamic means that types are inferred at runtime but you can’t mix types. For example, a = 1 + '0' will raise an error in Python. On the other hand, JavaScript is weak and dynamic, so types are inferred at runtime and you can mix types. For example, a = 1 + '0' will set a to 10.

While dynamic typing brings flexibility, it isn’t always desirable so, there have been a number of efforts as of late to bring static type inference to dynamic languages.

In this article, we’ll look at what type hints are and how they can benefit you. We’ll also dive into how you can use Python’s type system for static type checking with mypy and runtime type checking with pydantic, marshmallow, and typeguard.

#python #web-development #programming #developer #testing

Python Type Checking
2.55 GEEK