PyDocs

Nav

Development Tools

NamedTuple

typing

class (source)
  • NamedTuple(typename, fields, /)
Typed version of collections.namedtuple.

Usage::

    class Employee(NamedTuple):
        name: str
        id: int

This is equivalent to::

    Employee = collections.namedtuple('Employee', ['name', 'id'])

The types for each field name can be retrieved by calling
annotationlib.get_annotations(Employee).  (The field names are also in
the _fields attribute, which is part of the namedtuple API.)
An alternative equivalent functional syntax is also accepted::

    Employee = NamedTuple('Employee', [('name', str), ('id', int)])
Changed in 3.6, 3.6.1, 3.8, 3.9, 3.11, 3.14
  • 3.6: Added support for PEP 526 variable annotation syntax.
  • 3.6.1: Added support for default values, methods, and docstrings.
  • 3.8: The _field_types and __annotations__ attributes are now regular dictionaries instead of instances of OrderedDict.
  • 3.9: Removed the _field_types attribute in favor of the more standard __annotations__ attribute which has the same information.
  • 3.9: NamedTuple is now a function rather than a class. It can still be used as a class base, as described above.
  • 3.11: Added support for generic namedtuples.
  • 3.14: Using super() (and the __class__ closure variable) in methods of NamedTuple subclasses is unsupported and causes a TypeError.
0 Examples
No examples for typing/NamedTuple.
See Also
No see-alsos for typing/NamedTuple
0 Notes
No notes for NamedTuple