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
9a359210
Commit
9a359210
authored
Jan 07, 2008
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleanup named tuple subclassing example.
parent
4d7e6702
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
13 deletions
+16
-13
Doc/library/collections.rst
Doc/library/collections.rst
+12
-10
Lib/collections.py
Lib/collections.py
+4
-3
No files found.
Doc/library/collections.rst
View file @
9a359210
...
...
@@ -365,7 +365,7 @@ they add the ability to access fields by name instead of position index.
The *fieldnames* are a single string with each fieldname separated by whitespace
and/or commas (for example 'x y' or 'x, y'). Alternatively, the *fieldnames*
can be specified
as a list
of strings (such as ['x', 'y']).
can be specified
with a sequence
of strings (such as ['x', 'y']).
Any valid Python identifier may be used for a fieldname except for names
starting with an underscore. Valid identifiers consist of letters, digits,
...
...
@@ -478,7 +478,7 @@ three additional methods and one attribute.
Point(x=33, y=22)
>>> for partnum, record in inventory.items():
... inventory[partnum] = record._replace(price=newprices[partnum], updated
=time.now())
inventory[partnum] = record._replace(price=newprices[partnum], timestamp
=time.now())
.. attribute:: somenamedtuple._fields
...
...
@@ -515,13 +515,15 @@ a fixed-width print format::
@property
def hypot(self):
return (self.x ** 2 + self.y ** 2) ** 0.5
def __
rep
r__(self):
return 'Point
(x=%.3f, y=%.3f, hypot=%.3f)
' % (self.x, self.y, self.hypot)
def __
st
r__(self):
return 'Point
: x=%6.3f y=%6.3f hypot=%6.3f
' % (self.x, self.y, self.hypot)
>>> 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)
>>> for p in Point(3,4), Point(14,5), Point(9./7,6):
print p
Point: x= 3.000 y= 4.000 hypot= 5.000
Point: x=14.000 y= 5.000 hypot=14.866
Point: x= 1.286 y= 6.000 hypot= 6.136
Another use for subclassing is to replace performance critcal methods with
faster versions that bypass error-checking and localize variable access::
...
...
@@ -531,8 +533,8 @@ faster versions that bypass error-checking and localize variable access::
def _replace(self, _map=map, **kwds):
return self._make(_map(kwds.pop, ('x', 'y'), self))
Default values can be implemented by
starting with a prototype instance
and customizing it with :meth:`_replace`
::
Default values can be implemented by
using :meth:`_replace`:: to
customize a prototype instance
::
>>> Account = namedtuple('Account', 'owner balance transaction_count')
>>> model_account = Account('<owner name>', 0.0, 0)
...
...
Lib/collections.py
View file @
9a359210
...
...
@@ -120,10 +120,11 @@ if __name__ == '__main__':
@
property
def
hypot
(
self
):
return
(
self
.
x
**
2
+
self
.
y
**
2
)
**
0.5
def
__
rep
r__
(
self
):
return
'Point
(x=%.3f, y=%.3f, hypot=%.3f)
'
%
(
self
.
x
,
self
.
y
,
self
.
hypot
)
def
__
st
r__
(
self
):
return
'Point
: x=%6.3f y=%6.3f hypot=%6.3f
'
%
(
self
.
x
,
self
.
y
,
self
.
hypot
)
print
Point
(
3
,
4
),
'
\
n
'
,
Point
(
2
,
5
),
'
\
n
'
,
Point
(
9.
/
7
,
6
)
for
p
in
Point
(
3
,
4
),
Point
(
14
,
5
),
Point
(
9.
/
7
,
6
):
print
p
class
Point
(
namedtuple
(
'Point'
,
'x y'
)):
'Point class with optimized _make() and _replace() without error-checking'
...
...
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