Types Flashcards

(14 cards)

1
Q

Explicitly define any type for x

A

x: Any

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Type for list of integers

Python 3.9+

A

x: list[int]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Type for set of integers

Python 3.9+

A

x: set[int]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Type for dict where keys are string and values are float

Python 3.9+

A

x: dict[str, float]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Type for typles of integer, string, and float (like (3, "yes", 7.5))

Python 3.9+

A

x: tuple[int, str, float] = (3, "yes", 7.5)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Type for typles of integers of variadic size

Python 3.9+

A

x: tuple[int, ...]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Type for list where elements are either integers or strings

Python 3.10+

A

x: list[int | str] = [3, 5, "test", "fun"]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Type for variable which can be string or None

A

x: str | None

Optional[X] # 3.9 and earlier

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Function with type annotations, taking two integer args and returning integer.

A
def plus(num1: int, num2: int) -> int:
    return num1 + num2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Type for x holding a function (or other object with \_\_call\_\_) taking two integers and returning float

A

x: Callable[[int, int], float] = f

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Type for generator yielding integers

A

def gen(n: int) -> Iterator[int]:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Debug: print what type mypy infers for an expression

A

reveal_type(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Override the inferred type of an expression

A

b = cast(list[int], a)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Add additional annotation to a type (framework dependent)

A

Annotated[str, "this is just metadata"]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly