Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
e53bd8e2
Commit
e53bd8e2
authored
Jan 28, 2017
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue 29310: Document typing.NamedTuple default argument syntax
parent
dc4ce0e0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
4 deletions
+18
-4
Doc/library/typing.rst
Doc/library/typing.rst
+18
-4
No files found.
Doc/library/typing.rst
View file @
e53bd8e2
...
...
@@ -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,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment