typing.NamedTuple
Add type hints to named tuples.
A Typed Alternative
The typing.NamedTuple base class is a modern, class-based way to define named tuples. It adds type hints and reads like a normal class, which most editors and type checkers understand better than the factory form.
Class-Based Syntax
Subclass NamedTuple and declare each field with a type annotation. The result is still a real tuple.
from typing import NamedTuple
class Point(NamedTuple):
x: int
y: int
p = Point(1, 2)
print(p)All lessons in this course
- The Enum Class
- IntEnum and Flag
- NamedTuple Basics
- typing.NamedTuple