summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2008-05-08 07:23:30 (GMT)
committerRaymond Hettinger <python@rcn.com>2008-05-08 07:23:30 (GMT)
commit5a9fed75bd1c9da401004fd84c72437c1404a122 (patch)
treedb08611131327c0383c86850b18c330c1ba4dc0d /Doc
parentcf98f03a62c1e85eff9067cc980b630b619a1fc1 (diff)
downloadcpython-5a9fed75bd1c9da401004fd84c72437c1404a122.zip
cpython-5a9fed75bd1c9da401004fd84c72437c1404a122.tar.gz
cpython-5a9fed75bd1c9da401004fd84c72437c1404a122.tar.bz2
Fix-up the enumerate type example and move it to the end.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/collections.rst19
1 files changed, 9 insertions, 10 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
index dbc47cb..54c08b8 100644
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -570,16 +570,6 @@ by the :mod:`csv` or :mod:`sqlite3` modules::
for emp in map(EmployeeRecord._make, cursor.fetchall()):
print emp.name, emp.title
-Named tuples can also be used to generate enumerated constants:
-
-.. testcode::
-
- def enum(*names):
- return namedtuple('Enum', ' '.join(names))(*range(len(names)))
-
- Status = enum('open', 'pending', 'closed')
- assert (0, 1, 2) == (Status.open, Status.pending, Status.closed)
-
In addition to the methods inherited from tuples, named tuples support
three additional methods and one attribute. To prevent conflicts with
field names, the method and attribute names start with an underscore.
@@ -674,6 +664,15 @@ customize a prototype instance:
>>> default_account = Account('<owner name>', 0.0, 0)
>>> johns_account = default_account._replace(owner='John')
+Enumerated constants can be implemented with named tuples, but it is simpler
+and more efficient to use a simple class declaration:
+
+ >>> Status = namedtuple('Status', 'open pending closed')._make(range(3))
+ >>> Status.open, Status.pending, Status.closed
+ (0, 1, 2)
+ >>> class Status:
+ ... open, pending, closed = range(3)
+
.. rubric:: Footnotes
.. [#] For information on the double-star-operator see