summaryrefslogtreecommitdiffstats
path: root/Doc/library/collections.rst
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2008-01-07 09:03:49 (GMT)
committerRaymond Hettinger <python@rcn.com>2008-01-07 09:03:49 (GMT)
commitdc55f35f387479888019538a429f38104a8bc3b0 (patch)
tree5582bf27e456e192338234976edba1a053e0f879 /Doc/library/collections.rst
parentfd1c24518be99e3e2d0891bf7d2c94fbd4d39367 (diff)
downloadcpython-dc55f35f387479888019538a429f38104a8bc3b0.zip
cpython-dc55f35f387479888019538a429f38104a8bc3b0.tar.gz
cpython-dc55f35f387479888019538a429f38104a8bc3b0.tar.bz2
Add another named tuple subclassing example.
Diffstat (limited to 'Doc/library/collections.rst')
-rw-r--r--Doc/library/collections.rst8
1 files changed, 8 insertions, 0 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
index d7d1083..564c45b 100644
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -527,6 +527,14 @@ a fixed-width print format:
Point(x=2.000, y=5.000, hypot=5.385)
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:
+
+ >>> class Point(namedtuple('Point', 'x y')):
+ _make = classmethod(tuple.__new__)
+ 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`: