0Pricing
Python Academy · Lesson

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

  1. The Enum Class
  2. IntEnum and Flag
  3. NamedTuple Basics
  4. typing.NamedTuple
← Back to Python Academy