]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Add example for use cases requiring default values.
authorRaymond Hettinger <python@rcn.com>
Thu, 15 Nov 2007 22:39:34 +0000 (22:39 +0000)
committerRaymond Hettinger <python@rcn.com>
Thu, 15 Nov 2007 22:39:34 +0000 (22:39 +0000)
Doc/library/collections.rst

index 89885abd76597180cdface47c213af339f4f794c..bdc0b86a59179a878ed7a463515cd957802df48e 100644 (file)
@@ -492,6 +492,15 @@ the :meth:`__repr__` method:
     >>> Point(x=10, y=20)
     Point(10.000, 20.000)
 
+Default values can be implemented by starting with a prototype instance
+and customizing it with :meth:`__replace__`:
+
+::
+
+    >>> Account = namedtuple('Account', 'owner balance transaction_count')
+    >>> model_account = Account('<owner name>', 0.0, 0)
+    >>> johns_account = model_account.__replace__(owner='John')
+
 .. rubric:: Footnotes
 
 .. [#] For information on the star-operator see