[Python-3000-checkins] r62058 - in python/branches/py3k: Doc/library/collections.rst

martin.v.loewis python-3000-checkins at python.org
Sun Mar 30 22:18:05 CEST 2008


Author: martin.v.loewis
Date: Sun Mar 30 22:18:05 2008
New Revision: 62058

Modified:
   python/branches/py3k/   (props changed)
   python/branches/py3k/Doc/library/collections.rst
Log:
Merged revisions 62007 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r62007 | georg.brandl | 2008-03-28 13:58:26 +0100 (Fr, 28 Mär 2008) | 2 lines
  
  #2502: add example how to do enum types with named tuples.
........


Modified: python/branches/py3k/Doc/library/collections.rst
==============================================================================
--- python/branches/py3k/Doc/library/collections.rst	(original)
+++ python/branches/py3k/Doc/library/collections.rst	Sun Mar 30 22:18:05 2008
@@ -547,6 +547,16 @@
    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.


More information about the Python-3000-checkins mailing list