From 410bfab9903c988d71563c8534f76d3e81137c9f Mon Sep 17 00:00:00 2001 From: Bastian Kleineidam Date: Fri, 28 Mar 2008 11:22:50 +0100 Subject: Add enum() example for named tuples. Signed-off-by: Bastian Kleineidam diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index f07ac25..4fd8313 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -567,6 +567,14 @@ 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:: + + 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. -- 1.5.4.4