Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
5a9fed75
Commit
5a9fed75
authored
May 08, 2008
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix-up the enumerate type example and move it to the end.
parent
cf98f03a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
10 deletions
+9
-10
Doc/library/collections.rst
Doc/library/collections.rst
+9
-10
No files found.
Doc/library/collections.rst
View file @
5a9fed75
...
...
@@ -570,16 +570,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.
...
...
@@ -674,6 +664,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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment