diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2008-03-30 20:18:05 (GMT) |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2008-03-30 20:18:05 (GMT) |
commit | 72c82a4d624bd82e232ae2bdb47821362f73c0ed (patch) | |
tree | ad4d58721ddfd78c76a202b58dc79f9a570c17b8 | |
parent | 6ff4b3ca7b5d958feee098b8999526186341a9fa (diff) | |
download | cpython-72c82a4d624bd82e232ae2bdb47821362f73c0ed.zip cpython-72c82a4d624bd82e232ae2bdb47821362f73c0ed.tar.gz cpython-72c82a4d624bd82e232ae2bdb47821362f73c0ed.tar.bz2 |
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.
........
-rw-r--r-- | Doc/library/collections.rst | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index 7966a2e..7827700 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -547,6 +547,16 @@ 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. |