[Python-checkins] r62853 - python/trunk/Doc/library/collections.rst

raymond.hettinger python-checkins at python.org
Thu May 8 09:23:31 CEST 2008


Author: raymond.hettinger
Date: Thu May  8 09:23:30 2008
New Revision: 62853

Log:
Fix-up the enumerate type example and move it to the end.

Modified:
   python/trunk/Doc/library/collections.rst

Modified: python/trunk/Doc/library/collections.rst
==============================================================================
--- python/trunk/Doc/library/collections.rst	(original)
+++ python/trunk/Doc/library/collections.rst	Thu May  8 09:23:30 2008
@@ -570,16 +570,6 @@
    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 @@
     >>> 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


More information about the Python-checkins mailing list