On this page
  1. collections.namedtuple
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

collections.namedtuple

Prev: collections.deque | Up: collections | Next: collections.OrderedDict
Rel: collections.ChainMap, collections.Counter, collections.defaultdict, collections.deque
Ref: docs.python.org — collections.namedtuple
Tags: #public #python3 #stdlib #collections


Import

import collections
from collections import namedtuple

Signature & Overview

collections.namedtuple(typename, field_names, *, rename=False, defaults=None, module=None)

Returns a new subclass of tuple with named fields.

Usage

from collections import namedtuple

Point = namedtuple("Point", ["x", "y"], defaults=[0])
p = Point(3)
assert p == (3, 0) and p.x == 3 and p.y == 0
assert p._replace(y=4) == Point(3, 4)

Gotchas

REPL Inspect

from collections import namedtuple
help(namedtuple)

Linked from

Related notes