0 votes
in Python by
What are type annotations? What are generic type annotations?

1 Answer

0 votes
by

While Python is a dynamically typed language, there is a way to annotate types for clarity purposes. These are the built-in types:

int

float

bool

str

bytes

Complex types are available from the typing module:

List

Set

Dict

Tuple

Optional

etc.

Here is how you would define a function with type annotations:

def func1(x: int, y: str) -> bool:

    return False

Generic type annotations are annotations that take another type as a parameter, allowing you to specify complex logic:

List[int]

Optional[List[int]]

Tuple[bool]

etc.

Note that these are only used for warnings and static type checking. You will not be guaranteed these types at runtime.

Related questions

+1 vote
asked Jan 30, 2022 in Python by sharadyadav1986
0 votes
asked Jun 29, 2020 in Python by Robindeniel
...