diff options
author | Raymond Hettinger <python@rcn.com> | 2008-01-07 04:24:49 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2008-01-07 04:24:49 (GMT) |
commit | b8e0072fec28e6c1a8398b12b5b40f82e878f8b9 (patch) | |
tree | c1637b5c017f7d921cb558da97ae85c6e73eab57 /Doc | |
parent | 4273222a68f9bcc2a3afda2bb9d3367705b5437d (diff) | |
download | cpython-b8e0072fec28e6c1a8398b12b5b40f82e878f8b9.zip cpython-b8e0072fec28e6c1a8398b12b5b40f82e878f8b9.tar.gz cpython-b8e0072fec28e6c1a8398b12b5b40f82e878f8b9.tar.bz2 |
Add subclassing example to docs for named tuples.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/collections.rst | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index 0205cf1..1f9e82d 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -510,15 +510,22 @@ When casting a dictionary to a named tuple, use the double-star-operator [#]_:: Point(x=11, y=22) Since a named tuple is a regular Python class, it is easy to add or change -functionality. For example, the display format can be changed by overriding -the :meth:`__repr__` method: +functionality with a subclass. Here is how to add a calculated field and +a custom fixed-width print format: :: - >>> Point = namedtuple('Point', 'x y') - >>> Point.__repr__ = lambda self: 'Point(%.3f, %.3f)' % self - >>> Point(x=11, y=22) - Point(11.000, 22.000) + >>> 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) + Point(x=3.000, y=4.000, hypot=5.000) + >>> Point(2, 5) + Point(x=2.000, y=5.000, hypot=5.385) Default values can be implemented by starting with a prototype instance and customizing it with :meth:`_replace`: |