Contents

collections.abc - Abstract Base Classes for Containers

Contents

This module provides abstract base classes that can be used to test whether a class provides a particular interface; for example, whether it is hashable or whether it is a mapping.

Related Packages & Articles

typing - Support for type hints

This module provides runtime support for type hints as specified by PEP 484, PEP 526, PEP 544, PEP 586, PEP 589, PEP 591, PEP 612 and PEP 613. The most fundamental support consists of the types Any, Union, Tuple, Callable, TypeVar, and Generic. For full specification please see PEP 484. For a simplified introduction to type hints see PEP 483.

import typing

# preferred
from typing import Dict, List, Union, Any

myvar: Dict = {}
# non-preferred
import typing

myvar: typing.Dict = {}

Code examples


from typing import Mapping, Dict, Any, Union

example1: Dict = {}

example2: Dict[str, Any]

example2: Mapping[str, int]

example3: Union[Dict[str, int], None]

example4: Union[str, None]

def hello(name: str) -> str:
    return f"Hello {name}"

Reference

collections - Container Datatypes

This module implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple.