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
f0886444
Commit
f0886444
authored
Jan 10, 2008
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Clarify how to add a field to a named tuple.
parent
da6aa78c
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
4 additions
and
1 deletion
+4
-1
Doc/library/collections.rst
Doc/library/collections.rst
+1
-1
Lib/collections.py
Lib/collections.py
+3
-0
No files found.
Doc/library/collections.rst
View file @
f0886444
...
...
@@ -538,7 +538,7 @@ faster versions that bypass error-checking and that localize variable access::
Subclassing is not useful for adding new, stored fields. Instead, simply
create a new named tuple type from the :attr:`_fields` attribute::
>>> P
ixel = namedtuple('Pixel', Point._fields + Color._fields
)
>>> P
oint3D = namedtuple('Point3D', Point._fields + ('z',)
)
Default values can be implemented by using :meth:`_replace` to
customize a prototype instance::
...
...
Lib/collections.py
View file @
f0886444
...
...
@@ -137,6 +137,9 @@ if __name__ == '__main__':
print
Point
(
11
,
22
).
_replace
(
x
=
100
)
Point3D
=
namedtuple
(
'Point3D'
,
Point
.
_fields
+
(
'z'
,))
print
Point3D
.
__doc__
import
doctest
TestResults
=
namedtuple
(
'TestResults'
,
'failed attempted'
)
print
TestResults
(
*
doctest
.
testmod
())
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