diff options
author | Christian Heimes <christian@cheimes.de> | 2008-05-08 17:18:53 +0000 |
---|---|---|
committer | Christian Heimes <christian@cheimes.de> | 2008-05-08 17:18:53 +0000 |
commit | f7babda9857e4b30816a3088ac8705808e1f436e (patch) | |
tree | a161ce846f4aa7c7920ef8ff3c79be5f65664025 /Doc/library | |
parent | 3d7b184348ac7ff65b6bbc66c7238159fc5ac787 (diff) | |
download | cpython-f7babda9857e4b30816a3088ac8705808e1f436e.tar.gz |
Merged revisions 62805,62811,62841-62842,62848-62849,62853-62854 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r62805 | christian.heimes | 2008-05-07 01:59:53 +0200 (Wed, 07 May 2008) | 1 line
Re-added getbuildinfo.c solution item
........
r62811 | benjamin.peterson | 2008-05-07 04:23:43 +0200 (Wed, 07 May 2008) | 2 lines
update .bzrignore
........
r62841 | christian.heimes | 2008-05-08 00:54:17 +0200 (Thu, 08 May 2008) | 1 line
Replace more float hacks with correct math functions
........
r62842 | benjamin.peterson | 2008-05-08 01:11:54 +0200 (Thu, 08 May 2008) | 2 lines
Practice EAFP, and revert 62787
........
r62848 | raymond.hettinger | 2008-05-08 06:35:20 +0200 (Thu, 08 May 2008) | 1 line
Frozensets do not benefit from autoconversion.
........
r62849 | raymond.hettinger | 2008-05-08 06:36:12 +0200 (Thu, 08 May 2008) | 1 line
The __all__ variable forgot to expose the gcd() function.
........
r62853 | raymond.hettinger | 2008-05-08 09:23:30 +0200 (Thu, 08 May 2008) | 1 line
Fix-up the enumerate type example and move it to the end.
........
r62854 | ronald.oussoren | 2008-05-08 12:34:39 +0200 (Thu, 08 May 2008) | 3 lines
Fix for issue 1770190: platform.mac_ver() now returns the right
version on OSX 10.4.10
........
Diffstat (limited to 'Doc/library')
-rw-r--r-- | Doc/library/collections.rst | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index cff07b96c2..e886d86224 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -550,16 +550,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. @@ -655,6 +645,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 |