summaryrefslogtreecommitdiffstats
path: root/Doc/library/collections.rst
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2008-01-08 02:24:15 (GMT)
committerRaymond Hettinger <python@rcn.com>2008-01-08 02:24:15 (GMT)
commitac5742e0fe13d318c8f9d6c043bcb5a821bb2045 (patch)
treef5da00c9c465381c85f845be640e041f5b029382 /Doc/library/collections.rst
parent581671419f62ebc6155ff47929a531a6904f3c19 (diff)
downloadcpython-ac5742e0fe13d318c8f9d6c043bcb5a821bb2045.zip
cpython-ac5742e0fe13d318c8f9d6c043bcb5a821bb2045.tar.gz
cpython-ac5742e0fe13d318c8f9d6c043bcb5a821bb2045.tar.bz2
Docs on named tuple's naming conventions and limits of subclassing
Diffstat (limited to 'Doc/library/collections.rst')
-rw-r--r--Doc/library/collections.rst8
1 files changed, 7 insertions, 1 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
index be6c67f..dde7f55 100644
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -446,7 +446,8 @@ by the :mod:`csv` or :mod:`sqlite3` modules::
print emp.name, emp.title
In addition to the methods inherited from tuples, named tuples support
-three additional methods and one attribute.
+three additional methods and one attribute. To prevent conflicts with
+field names, the method and attribute names start with an underscore.
.. method:: somenamedtuple._make(iterable)
@@ -533,6 +534,11 @@ faster versions that bypass error-checking and that localize variable access::
def _replace(self, _map=map, **kwds):
return self._make(_map(kwds.get, ('x', 'y'), self))
+Subclassing is not useful for adding new, stored fields. Instead, simply
+create a new named tuple type from the :attr:`_fields` attribute::
+
+ >>> Pixel = namedtuple('Pixel', Point._fields + Color._fields)
+
Default values can be implemented by using :meth:`_replace` to
customize a prototype instance::