Python Built-in Types


ByteArray

A mutable bytes object. The following are equivalent declarations:

ba = bytearray(b'...'))
ba = bytearray([46, 46, 46]).

Note that iterating over a bytearray yeilds int values, not byte values.

In the context of type annotations, try:

ba: bytes = bytearray(b'abc'))


Bytes

In the context of type annotations, try:

b: bytes = b"abc"


Complex


Dict

In the context of type annotations, try:

d: dict[str, int] = {"1": 1}

Float


FrozenSet

In the context of type annotations, try:

def __init__(self) --> None:
    self._options: frozenset[str] = frozenset([1, 2])


Int


List

In the context of type annotations, try:

l: list[str] = ["ham", "spam"]


MemoryView

An interface into internal memory of objects.

In the context of type annotations, try:

mv: bytes = memoryview(obj)


Set

In the context of type annotations, try:

s: set[str] = set(["A", "B", "A"])


Str


Tuple

In the context of type annotations, try:

single: tuple[int] = [1]
double: tuple[int, int] = [1, 2]
nple: tuple[int, ...] = [1, 2, 3, 4, 5]

The tuple[T, ...] annotation checks for homogenous typing. To allow for a a variable number of any values, try:

from typing import Any

something: tuple[Any, ...] = [1, "1"]


CategoryRicottone