diff options
author | Raymond Hettinger <python@rcn.com> | 2017-01-29 04:16:40 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2017-01-29 04:16:40 (GMT) |
commit | e53bd8e2d0787465e888b407aeabb8a7ad24147d (patch) | |
tree | e62af4ee7cee3beccfa82c4b184eeb6c51869d76 /Doc | |
parent | dc4ce0e0139cbe8867e7bf235ba6af796129a7d0 (diff) | |
download | cpython-e53bd8e2d0787465e888b407aeabb8a7ad24147d.zip cpython-e53bd8e2d0787465e888b407aeabb8a7ad24147d.tar.gz cpython-e53bd8e2d0787465e888b407aeabb8a7ad24147d.tar.bz2 |
Issue 29310: Document typing.NamedTuple default argument syntax
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/typing.rst | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index cd59d10..efae67a 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -735,10 +735,21 @@ The module defines the following classes, functions and decorators: Employee = collections.namedtuple('Employee', ['name', 'id']) - The resulting class has one extra attribute: ``_field_types``, - giving a dict mapping field names to types. (The field names - are in the ``_fields`` attribute, which is part of the namedtuple - API.) + To give a field a default value, you can assign to it in the class body:: + + class Employee(NamedTuple): + 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:: @@ -747,6 +758,9 @@ The module defines the following classes, functions and decorators: .. versionchanged:: 3.6 Added support for :pep:`526` variable annotation syntax. + .. versionchanged:: 3.6.1 + Added support for default values. + .. function:: NewType(typ) A helper function to indicate a distinct types to a typechecker, |