Typing and Generics

typing.NamedTuple

Creates a tuple subclass with named fields and annotations. Field bindings are immutable, but contained values can be mutable; annotations are not runtime validators. Fields with defaults follow non-default fields.

Formal Syntax & Structure

python syntaxpython
class Name(typing.NamedTuple): ...

Return Value

tuple subclass

Typing construct or runtime result as described; static typing rules are not runtime validation.

Working Example

Example Snippetpython
from typing import NamedTuple
class Point(NamedTuple):    x: int    y: int = 0p=Point(4)print((p.x, p.y, tuple(p)))
Python Reference