summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2008-01-07 04:24:49 (GMT)
committerRaymond Hettinger <python@rcn.com>2008-01-07 04:24:49 (GMT)
commitb8e0072fec28e6c1a8398b12b5b40f82e878f8b9 (patch)
treec1637b5c017f7d921cb558da97ae85c6e73eab57 /Lib
parent4273222a68f9bcc2a3afda2bb9d3367705b5437d (diff)
downloadcpython-b8e0072fec28e6c1a8398b12b5b40f82e878f8b9.zip
cpython-b8e0072fec28e6c1a8398b12b5b40f82e878f8b9.tar.gz
cpython-b8e0072fec28e6c1a8398b12b5b40f82e878f8b9.tar.bz2
Add subclassing example to docs for named tuples.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/collections.py11
1 files changed, 9 insertions, 2 deletions
diff --git a/Lib/collections.py b/Lib/collections.py
index 0b86898..1701952 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -116,8 +116,15 @@ if __name__ == '__main__':
assert p == loads(dumps(p))
# test and demonstrate ability to override methods
- Point.__repr__ = lambda self: 'Point(%.3f, %.3f)' % self
- print p
+ class Point(namedtuple('Point', 'x y')):
+ @property
+ def hypot(self):
+ return (self.x ** 2 + self.y ** 2) ** 0.5
+ 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)
import doctest
TestResults = namedtuple('TestResults', 'failed attempted')