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
80ab1d2b
Commit
80ab1d2b
authored
Jan 07, 2008
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleanup subclassing example to more clearly show fixed-width print format.
parent
6d0c6bd1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
6 additions
and
7 deletions
+6
-7
Doc/library/collections.rst
Doc/library/collections.rst
+5
-5
Lib/collections.py
Lib/collections.py
+1
-2
No files found.
Doc/library/collections.rst
View file @
80ab1d2b
...
...
@@ -511,7 +511,7 @@ When casting a dictionary to a named tuple, use the double-star-operator [#]_::
Since a named tuple is a regular Python class, it is easy to add or change
functionality with a subclass. Here is how to add a calculated field and
a
custom
fixed-width print format:
a fixed-width print format:
::
...
...
@@ -522,10 +522,10 @@ a custom fixed-width print format:
def __repr__(self):
return 'Point(x=%.3f, y=%.3f, hypot=%.3f)' % (self.x, self.y, self.hypot)
>>> print Point(3, 4)
Point(x=3.000, y=4.000, hypot=5.000)
>>> Point(2, 5)
Point(x=
2.000, y=5.000, hypot=5.385
)
>>> print Point(3, 4)
,'\n', Point(2, 5), '\n', Point(9./7, 6)
Point(x=3.000, y=4.000, hypot=5.000)
Point(x=2.000, y=5.000, hypot=5.385)
Point(x=
1.286, y=6.000, hypot=6.136
)
Default values can be implemented by starting with a prototype instance
and customizing it with :meth:`_replace`:
...
...
Lib/collections.py
View file @
80ab1d2b
...
...
@@ -123,8 +123,7 @@ if __name__ == '__main__':
def
__repr__
(
self
):
return
'Point(x=%.3f, y=%.3f, hypot=%.3f)'
%
(
self
.
x
,
self
.
y
,
self
.
hypot
)
print
Point
(
3
,
4
)
print
Point
(
2
,
5
)
print
Point
(
3
,
4
),
'
\
n
'
,
Point
(
2
,
5
),
'
\
n
'
,
Point
(
9.
/
7
,
6
)
import
doctest
TestResults
=
namedtuple
(
'TestResults'
,
'failed attempted'
)
...
...
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