diff options
author | Raymond Hettinger <python@rcn.com> | 2007-12-14 21:51:50 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2007-12-14 21:51:50 (GMT) |
commit | e846f38c77f08dba5efd5497d28c741434153a69 (patch) | |
tree | 00dd69125159f90ef4abd9ff33015e77378b4c5c | |
parent | 07ae83f8400702a78825204751604b98b6f69c65 (diff) | |
download | cpython-e846f38c77f08dba5efd5497d28c741434153a69.zip cpython-e846f38c77f08dba5efd5497d28c741434153a69.tar.gz cpython-e846f38c77f08dba5efd5497d28c741434153a69.tar.bz2 |
Add usage note
-rw-r--r-- | Doc/library/collections.rst | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index d782c69..771310d 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -488,6 +488,14 @@ two additonal methods and a read-only attribute. >>> Pixel(11, 22, 128, 255, 0) Pixel(x=11, y=22, red=128, green=255, blue=0)' +To retrieve a field whose name is stored in a string, use the :func:`getattr` +function: + +:: + + >>> getattr(p, 'x') + 11 + 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: @@ -496,8 +504,8 @@ the :meth:`__repr__` method: >>> Point = namedtuple('Point', 'x y') >>> Point.__repr__ = lambda self: 'Point(%.3f, %.3f)' % self - >>> Point(x=10, y=20) - Point(10.000, 20.000) + >>> Point(x=11, y=22) + Point(11.000, 22.000) Default values can be implemented by starting with a prototype instance and customizing it with :meth:`_replace`: |