Issue 29310: Document typing.NamedTuple default argument syntax

This commit is contained in:
Raymond Hettinger 2017-01-28 20:16:40 -08:00
parent dc4ce0e013
commit e53bd8e2d0
1 changed files with 18 additions and 4 deletions

View File

@ -735,10 +735,21 @@ The module defines the following classes, functions and decorators:
Employee = collections.namedtuple('Employee', ['name', 'id']) Employee = collections.namedtuple('Employee', ['name', 'id'])
The resulting class has one extra attribute: ``_field_types``, To give a field a default value, you can assign to it in the class body::
giving a dict mapping field names to types. (The field names
are in the ``_fields`` attribute, which is part of the namedtuple class Employee(NamedTuple):
API.) name: str
id: int = 3
employee = Employee('Guido')
assert employee.id == 3
Fields with a default value must come after any fields without a default.
The resulting class has two extra attributes: ``_field_types``,
giving a dict mapping field names to types, and ``field_defaults``, a dict
mapping field names to default values. (The field names are in the
``_fields`` attribute, which is part of the namedtuple API.)
Backward-compatible usage:: Backward-compatible usage::
@ -747,6 +758,9 @@ The module defines the following classes, functions and decorators:
.. versionchanged:: 3.6 .. versionchanged:: 3.6
Added support for :pep:`526` variable annotation syntax. Added support for :pep:`526` variable annotation syntax.
.. versionchanged:: 3.6.1
Added support for default values.
.. function:: NewType(typ) .. function:: NewType(typ)
A helper function to indicate a distinct types to a typechecker, A helper function to indicate a distinct types to a typechecker,