In Python for parameter typing, do you use the capital versions of dict and list?
Question:
Apologies, am new to Python so a very basic question. Below is an example line of my method definition. Where would I be using capital Dict/List and lowercase Dict/List?
Thanks in advance!
Example scenarios below
def execute_signals(self, parameter1: dict[list], parameter2: dict) -> list[dict]:
def execute_signals(self, parameter1: Dict[List], parameter2: Dict) -> List[Dict]:
def execute_signals(self, parameter1: dict[List], parameter2: dict) -> List[dict]:
Answers:
The List
or Dict
have to be imported from the typing
module.
Before python 3.9 it was necessary, cause built-in list
wouldn’t allow you to do something like this:
list[int]
It would be a syntax error. It was introduced in Python 3.9, so if you are using that (or newer version) you can use lowercase.
It’s also worth to take a look at this: https://peps.python.org/pep-0585/
Starting with Python 3.7, when from __future__ import annotations
is used, function and variable annotations can parameterize standard collections directly.
On Python 3.8 and earlier, the name of the collection type is capitalized, and the type is imported from the typing
module.
Python 3.5 – 3.8 (also works in Python 3.9+):
from typing import List, Set, Dict, Tuple
x: List[int] = [1]
x: Set[int] = {6, 7}
x: Dict[str, float] = {"field": 2.0}
x: Tuple[int, str, float] = (3, "yes", 7.5)
x: Tuple[int, ...] = (1, 2, 3)
Python 3.9+:
x: list[int] = [1]
x: set[int] = {6, 7}
x: dict[str, float] = {"field": 2.0}
x: tuple[int, str, float] = (3, "yes", 7.5)
x: tuple[int, ...] = (1, 2, 3)
I recommend you read through the mypy’s docs about type hints.
In Python, the capital versions of Dict
and List
refer to the abstract base classes for dictionaries and lists, respectively. These classes are part of the collections.abc
module and are used primarily for type checking and duck typing.
However, in function annotations or parameter typing, it’s more common to use the lowercase versions of dict
and list
, as they refer to the concrete classes. For example:
def my_function(my_list: list, my_dict: dict) -> int:
# function body here
This function takes a list and a dictionary as arguments, and returns an integer. Note that we are using the lowercase versions of list
and dict
for parameter typing.
Apologies, am new to Python so a very basic question. Below is an example line of my method definition. Where would I be using capital Dict/List and lowercase Dict/List?
Thanks in advance!
Example scenarios below
def execute_signals(self, parameter1: dict[list], parameter2: dict) -> list[dict]:
def execute_signals(self, parameter1: Dict[List], parameter2: Dict) -> List[Dict]:
def execute_signals(self, parameter1: dict[List], parameter2: dict) -> List[dict]:
The List
or Dict
have to be imported from the typing
module.
Before python 3.9 it was necessary, cause built-in list
wouldn’t allow you to do something like this:
list[int]
It would be a syntax error. It was introduced in Python 3.9, so if you are using that (or newer version) you can use lowercase.
It’s also worth to take a look at this: https://peps.python.org/pep-0585/
Starting with Python 3.7, when from __future__ import annotations
is used, function and variable annotations can parameterize standard collections directly.
On Python 3.8 and earlier, the name of the collection type is capitalized, and the type is imported from the typing
module.
Python 3.5 – 3.8 (also works in Python 3.9+):
from typing import List, Set, Dict, Tuple
x: List[int] = [1]
x: Set[int] = {6, 7}
x: Dict[str, float] = {"field": 2.0}
x: Tuple[int, str, float] = (3, "yes", 7.5)
x: Tuple[int, ...] = (1, 2, 3)
Python 3.9+:
x: list[int] = [1]
x: set[int] = {6, 7}
x: dict[str, float] = {"field": 2.0}
x: tuple[int, str, float] = (3, "yes", 7.5)
x: tuple[int, ...] = (1, 2, 3)
I recommend you read through the mypy’s docs about type hints.
In Python, the capital versions of Dict
and List
refer to the abstract base classes for dictionaries and lists, respectively. These classes are part of the collections.abc
module and are used primarily for type checking and duck typing.
However, in function annotations or parameter typing, it’s more common to use the lowercase versions of dict
and list
, as they refer to the concrete classes. For example:
def my_function(my_list: list, my_dict: dict) -> int:
# function body here
This function takes a list and a dictionary as arguments, and returns an integer. Note that we are using the lowercase versions of list
and dict
for parameter typing.