Intro.
A dynamically-typed language is a programming language where the type of a variable is determined and checked at runtime, rather than during compilation. In dynamically-typed languages, variables are not required to have their types explicitly declared; instead, their types are inferred based on the value assigned to them.
A type-annotated function:
Refers to a function definition that includes type hints or annotations, specifying the expected types of the function's parameters and the type of the value it returns
Define and annotate variables specified values:
simply assign the value to the variable with the type annotation syntax. The syntax for variable annotation in Python is variable_name: Type = value
. Here are some examples:
age: int = 30
name: str = "Alice"
scores: List[float] = [90.5, 78.0, 82.5]
is_active: bool = True
coordinates: Tuple[int, int] = (10, 20)
Another Example:
To define a function that takes a list of floats as an argument, you can use the List
generic class from the typing
module in Python. This class allows you to specify that a variable should be a list of a specific type. In this case, you want a list of floats, so you would use List[float]
.
Here's an example of a function that takes a list of floats as an argument:
from typing import List
def process_floats(numbers: List[float]) -> None:
"""Process a list of floats."""
for number in numbers:
print(number)
UNION:
In Python, you can use the Union
type hint from the typing
module to specify that a function parameter can be one of several types. In your case, you want a function that takes a list of integers and floats. You can achieve this by using Union[List[int], List[float]]
as the type hint for the function parameter.
Here's an example:
The choice between
Union[List[int], List[float]]
andList[Union[int, float]]
depends on whether you want to allow a list that contains both integers and floats, or whether you want to enforce that all elements in the list are of the same type (all integers or all floats).If you use
Union[List[int], List[float]]
, the function expects either a list of integers or a list of floats, but not a mix of both
Callable:
In Python, Callable
is a type hint that represents any callable object, including functions, methods, and classes. It's part of the typing
module and can be used to annotate function parameters and return values.
When used as a type hint, Callable
typically requires two parameters: the argument types and the return type of the callable.
Here's an example of a function that takes a callable as an argument:
Here's an example of a function that takes a callable as an argument:

func
is expected to be a callable that takes an integer as an argument and returns an integer.
You can call apply_func
with a function that meets this requirement, like so:
result = apply_func(5, lambda x: x * x) print(result) # Outputs: 25
Duck-typed annotations
Duck typing is a concept in Python where the type or the class of an object is less important than the methods it defines. If an object behaves like a duck (it walks like a duck, it quacks like a duck), then it's a duck. This is a form of dynamic typing where the type compatibility is checked during runtime instead of compile time
Any:
The Any
type hint in Python is a special type that can represent any type. It's part of the typing
module. When used as a type hint, Any
essentially turns off the type checker. This is because Any
is compatible with every type - it can be an integer, a string, a list, or any other type.
Here's an example of a function with Any
type hint:
from typing import Any
def process_value(value: Any) -> None: print(value)
To validate your Python code with mypy
, follow these steps:
Install mypy if you haven't already. You can install it using pip:
pip3 install mypy
mypy performs static type checking, which means it checks for errors without actually running your code.