blob: 8278ce49318acff85d17d074d0b645c663521ffe [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`collections` --- High-performance container datatypes
3===========================================================
4
5.. module:: collections
6 :synopsis: High-performance datatypes
7.. moduleauthor:: Raymond Hettinger <python@rcn.com>
8.. sectionauthor:: Raymond Hettinger <python@rcn.com>
9
10
Georg Brandl116aa622007-08-15 14:28:22 +000011This module implements high-performance container datatypes. Currently,
12there are two datatypes, :class:`deque` and :class:`defaultdict`, and
Raymond Hettingerebcee3f2008-02-06 19:54:00 +000013one datatype factory function, :func:`namedtuple`.
Georg Brandl116aa622007-08-15 14:28:22 +000014
Raymond Hettingerebcee3f2008-02-06 19:54:00 +000015The specialized containers provided in this module provide alternatives
16to Python's general purpose built-in containers, :class:`dict`,
17:class:`list`, :class:`set`, and :class:`tuple`.
18
19Besides the containers provided here, the optional :mod:`bsddb`
20module offers the ability to create in-memory or file based ordered
21dictionaries with string keys using the :meth:`bsddb.btopen` method.
Georg Brandl116aa622007-08-15 14:28:22 +000022
Mark Summerfield08898b42007-09-05 08:43:04 +000023In addition to containers, the collections module provides some ABCs
Raymond Hettingerebcee3f2008-02-06 19:54:00 +000024(abstract base classes) that can be used to test whether a class
25provides a particular interface, for example, is it hashable or
26a mapping.
27
28ABCs - abstract base classes
29----------------------------
30
31The collections module offers the following ABCs:
Mark Summerfield08898b42007-09-05 08:43:04 +000032
Raymond Hettinger409fb2c2008-02-09 02:17:06 +000033========================= ==================== ====================== ====================================================
34ABC Inherits Abstract Methods Mixin Methods
35========================= ==================== ====================== ====================================================
36:class:`Container` ``__contains__``
37:class:`Hashable` ``__hash__``
38:class:`Iterable` ``__iter__``
39:class:`Iterator` :class:`Iterable` ``__next__`` ``__iter__``
40:class:`Sized` ``__len__``
41
42:class:`Mapping` :class:`Sized`, ``__getitem__``, ``__contains__``, ``keys``, ``items``, ``values``,
43 :class:`Iterable`, ``__len__``. and ``get``, ``__eq__``, and ``__ne__``
44 :class:`Container` ``__iter__``
45
46:class:`MutableMapping` :class:`Mapping` ``__getitem__`` Inherited Mapping methods and
47 ``__setitem__``, ``pop``, ``popitem``, ``clear``, ``update``,
48 ``__delitem__``, and ``setdefault``
49 ``__iter__``, and
50 ``__len__``
51
52:class:`Sequence` :class:`Sized`, ``__getitem__`` ``__contains__``. ``__iter__``, ``__reversed__``.
53 :class:`Iterable`, and ``__len__`` ``index``, and ``count``
54 :class:`Container`
55
56:class:`MutableSequnce` :class:`Sequence` ``__getitem__`` Inherited Sequence methods and
57 ``__delitem__``, ``append``, ``reverse``, ``extend``, ``pop``,
58 ``insert``, ``remove``, and ``__iadd__``
59 and ``__len__``
60
61:class:`Set` :class:`Sized`, ``__len__``, ``__le__``, ``__lt__``, ``__eq__``, ``__ne__``,
62 :class:`Iterable`, ``__iter__``, and ``__gt__``, ``__ge__``, ``__and__``, ``__or__``
63 :class:`Container` ``__contains__`` ``__sub__``, ``__xor__``, and ``isdisjoint``
64
65:class:`MutableSet` :class:`Set` ``add`` and Inherited Set methods and
66 ``discard`` ``clear``, ``pop``, ``remove``, ``__ior__``,
67 ``__iand__``, ``__ixor__``, and ``__isub__``
68========================= ==================== ====================== ====================================================
Mark Summerfield08898b42007-09-05 08:43:04 +000069
Mark Summerfield08898b42007-09-05 08:43:04 +000070These ABCs allow us to ask classes or instances if they provide
71particular functionality, for example::
72
Mark Summerfield08898b42007-09-05 08:43:04 +000073 size = None
Raymond Hettingerebcee3f2008-02-06 19:54:00 +000074 if isinstance(myvar, collections.Sized):
Mark Summerfield08898b42007-09-05 08:43:04 +000075 size = len(myvar)
76
Raymond Hettingerebcee3f2008-02-06 19:54:00 +000077Several of the ABCs are also useful as mixins that make it easier to develop
78classes supporting container APIs. For example, to write a class supporting
79the full :class:`Set` API, it only necessary to supply the three underlying
80abstract methods: :meth:`__contains__`, :meth:`__iter__`, and :meth:`__len__`.
81The ABC supplies the remaining methods such as :meth:`__and__` and
82:meth:`isdisjoint` ::
83
84 class ListBasedSet(collections.Set):
Raymond Hettingerc1b6a4a2008-02-08 23:46:23 +000085 ''' Alternate set implementation favoring space over speed
86 and not requiring the set elements to be hashable. '''
Raymond Hettingerebcee3f2008-02-06 19:54:00 +000087 def __init__(self, iterable):
Raymond Hettingerc1b6a4a2008-02-08 23:46:23 +000088 self.elements = lst = []
89 for value in iterable:
90 if value not in lst:
91 lst.append(value)
Raymond Hettingerebcee3f2008-02-06 19:54:00 +000092 def __iter__(self):
93 return iter(self.elements)
94 def __contains__(self, value):
95 return value in self.elements
96 def __len__(self):
97 return len(self.elements)
98
99 s1 = ListBasedSet('abcdef')
100 s2 = ListBasedSet('defghi')
101 overlap = s1 & s2 # The __and__() method is supported automatically
102
103
Mark Summerfield08898b42007-09-05 08:43:04 +0000104(For more about ABCs, see the :mod:`abc` module and :pep:`3119`.)
105
106
Georg Brandl116aa622007-08-15 14:28:22 +0000107
108.. _deque-objects:
109
110:class:`deque` objects
111----------------------
112
113
Georg Brandl9afde1c2007-11-01 20:32:30 +0000114.. class:: deque([iterable[, maxlen]])
Georg Brandl116aa622007-08-15 14:28:22 +0000115
116 Returns a new deque object initialized left-to-right (using :meth:`append`) with
117 data from *iterable*. If *iterable* is not specified, the new deque is empty.
118
119 Deques are a generalization of stacks and queues (the name is pronounced "deck"
120 and is short for "double-ended queue"). Deques support thread-safe, memory
121 efficient appends and pops from either side of the deque with approximately the
122 same O(1) performance in either direction.
123
124 Though :class:`list` objects support similar operations, they are optimized for
125 fast fixed-length operations and incur O(n) memory movement costs for
126 ``pop(0)`` and ``insert(0, v)`` operations which change both the size and
127 position of the underlying data representation.
128
Georg Brandl116aa622007-08-15 14:28:22 +0000129
Georg Brandl9afde1c2007-11-01 20:32:30 +0000130 If *maxlen* is not specified or is *None*, deques may grow to an
131 arbitrary length. Otherwise, the deque is bounded to the specified maximum
132 length. Once a bounded length deque is full, when new items are added, a
133 corresponding number of items are discarded from the opposite end. Bounded
134 length deques provide functionality similar to the ``tail`` filter in
135 Unix. They are also useful for tracking transactions and other pools of data
136 where only the most recent activity is of interest.
137
Georg Brandl9afde1c2007-11-01 20:32:30 +0000138
Georg Brandl116aa622007-08-15 14:28:22 +0000139Deque objects support the following methods:
140
Georg Brandl116aa622007-08-15 14:28:22 +0000141.. method:: deque.append(x)
142
143 Add *x* to the right side of the deque.
144
145
146.. method:: deque.appendleft(x)
147
148 Add *x* to the left side of the deque.
149
150
151.. method:: deque.clear()
152
153 Remove all elements from the deque leaving it with length 0.
154
155
156.. method:: deque.extend(iterable)
157
158 Extend the right side of the deque by appending elements from the iterable
159 argument.
160
161
162.. method:: deque.extendleft(iterable)
163
164 Extend the left side of the deque by appending elements from *iterable*. Note,
165 the series of left appends results in reversing the order of elements in the
166 iterable argument.
167
168
169.. method:: deque.pop()
170
171 Remove and return an element from the right side of the deque. If no elements
172 are present, raises an :exc:`IndexError`.
173
174
175.. method:: deque.popleft()
176
177 Remove and return an element from the left side of the deque. If no elements are
178 present, raises an :exc:`IndexError`.
179
180
181.. method:: deque.remove(value)
182
183 Removed the first occurrence of *value*. If not found, raises a
184 :exc:`ValueError`.
185
Georg Brandl116aa622007-08-15 14:28:22 +0000186
187.. method:: deque.rotate(n)
188
189 Rotate the deque *n* steps to the right. If *n* is negative, rotate to the
190 left. Rotating one step to the right is equivalent to:
191 ``d.appendleft(d.pop())``.
192
193In addition to the above, deques support iteration, pickling, ``len(d)``,
194``reversed(d)``, ``copy.copy(d)``, ``copy.deepcopy(d)``, membership testing with
195the :keyword:`in` operator, and subscript references such as ``d[-1]``.
196
197Example::
198
199 >>> from collections import deque
200 >>> d = deque('ghi') # make a new deque with three items
201 >>> for elem in d: # iterate over the deque's elements
Georg Brandl6911e3c2007-09-04 07:15:32 +0000202 ... print(elem.upper())
Georg Brandl116aa622007-08-15 14:28:22 +0000203 G
204 H
205 I
206
207 >>> d.append('j') # add a new entry to the right side
208 >>> d.appendleft('f') # add a new entry to the left side
209 >>> d # show the representation of the deque
210 deque(['f', 'g', 'h', 'i', 'j'])
211
212 >>> d.pop() # return and remove the rightmost item
213 'j'
214 >>> d.popleft() # return and remove the leftmost item
215 'f'
216 >>> list(d) # list the contents of the deque
217 ['g', 'h', 'i']
218 >>> d[0] # peek at leftmost item
219 'g'
220 >>> d[-1] # peek at rightmost item
221 'i'
222
223 >>> list(reversed(d)) # list the contents of a deque in reverse
224 ['i', 'h', 'g']
225 >>> 'h' in d # search the deque
226 True
227 >>> d.extend('jkl') # add multiple elements at once
228 >>> d
229 deque(['g', 'h', 'i', 'j', 'k', 'l'])
230 >>> d.rotate(1) # right rotation
231 >>> d
232 deque(['l', 'g', 'h', 'i', 'j', 'k'])
233 >>> d.rotate(-1) # left rotation
234 >>> d
235 deque(['g', 'h', 'i', 'j', 'k', 'l'])
236
237 >>> deque(reversed(d)) # make a new deque in reverse order
238 deque(['l', 'k', 'j', 'i', 'h', 'g'])
239 >>> d.clear() # empty the deque
240 >>> d.pop() # cannot pop from an empty deque
241 Traceback (most recent call last):
242 File "<pyshell#6>", line 1, in -toplevel-
243 d.pop()
244 IndexError: pop from an empty deque
245
246 >>> d.extendleft('abc') # extendleft() reverses the input order
247 >>> d
248 deque(['c', 'b', 'a'])
249
250
251.. _deque-recipes:
252
Georg Brandl9afde1c2007-11-01 20:32:30 +0000253:class:`deque` Recipes
254^^^^^^^^^^^^^^^^^^^^^^
Georg Brandl116aa622007-08-15 14:28:22 +0000255
256This section shows various approaches to working with deques.
257
258The :meth:`rotate` method provides a way to implement :class:`deque` slicing and
259deletion. For example, a pure python implementation of ``del d[n]`` relies on
260the :meth:`rotate` method to position elements to be popped::
261
262 def delete_nth(d, n):
263 d.rotate(-n)
264 d.popleft()
265 d.rotate(n)
266
267To implement :class:`deque` slicing, use a similar approach applying
268:meth:`rotate` to bring a target element to the left side of the deque. Remove
269old entries with :meth:`popleft`, add new entries with :meth:`extend`, and then
270reverse the rotation.
Georg Brandl116aa622007-08-15 14:28:22 +0000271With minor variations on that approach, it is easy to implement Forth style
272stack manipulations such as ``dup``, ``drop``, ``swap``, ``over``, ``pick``,
273``rot``, and ``roll``.
274
Georg Brandl116aa622007-08-15 14:28:22 +0000275Multi-pass data reduction algorithms can be succinctly expressed and efficiently
276coded by extracting elements with multiple calls to :meth:`popleft`, applying
Georg Brandl9afde1c2007-11-01 20:32:30 +0000277a reduction function, and calling :meth:`append` to add the result back to the
278deque.
Georg Brandl116aa622007-08-15 14:28:22 +0000279
280For example, building a balanced binary tree of nested lists entails reducing
281two adjacent nodes into one by grouping them in a list::
282
283 >>> def maketree(iterable):
284 ... d = deque(iterable)
285 ... while len(d) > 1:
286 ... pair = [d.popleft(), d.popleft()]
287 ... d.append(pair)
288 ... return list(d)
289 ...
Georg Brandl6911e3c2007-09-04 07:15:32 +0000290 >>> print(maketree('abcdefgh'))
Georg Brandl116aa622007-08-15 14:28:22 +0000291 [[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]]
292
Georg Brandl9afde1c2007-11-01 20:32:30 +0000293Bounded length deques provide functionality similar to the ``tail`` filter
294in Unix::
Georg Brandl116aa622007-08-15 14:28:22 +0000295
Georg Brandl9afde1c2007-11-01 20:32:30 +0000296 def tail(filename, n=10):
297 'Return the last n lines of a file'
298 return deque(open(filename), n)
Georg Brandl116aa622007-08-15 14:28:22 +0000299
300.. _defaultdict-objects:
301
302:class:`defaultdict` objects
303----------------------------
304
305
306.. class:: defaultdict([default_factory[, ...]])
307
308 Returns a new dictionary-like object. :class:`defaultdict` is a subclass of the
309 builtin :class:`dict` class. It overrides one method and adds one writable
310 instance variable. The remaining functionality is the same as for the
311 :class:`dict` class and is not documented here.
312
313 The first argument provides the initial value for the :attr:`default_factory`
314 attribute; it defaults to ``None``. All remaining arguments are treated the same
315 as if they were passed to the :class:`dict` constructor, including keyword
316 arguments.
317
Georg Brandl116aa622007-08-15 14:28:22 +0000318
319:class:`defaultdict` objects support the following method in addition to the
320standard :class:`dict` operations:
321
Georg Brandl116aa622007-08-15 14:28:22 +0000322.. method:: defaultdict.__missing__(key)
323
324 If the :attr:`default_factory` attribute is ``None``, this raises an
325 :exc:`KeyError` exception with the *key* as argument.
326
327 If :attr:`default_factory` is not ``None``, it is called without arguments to
328 provide a default value for the given *key*, this value is inserted in the
329 dictionary for the *key*, and returned.
330
331 If calling :attr:`default_factory` raises an exception this exception is
332 propagated unchanged.
333
334 This method is called by the :meth:`__getitem__` method of the :class:`dict`
335 class when the requested key is not found; whatever it returns or raises is then
336 returned or raised by :meth:`__getitem__`.
337
338:class:`defaultdict` objects support the following instance variable:
339
340
341.. attribute:: defaultdict.default_factory
342
343 This attribute is used by the :meth:`__missing__` method; it is initialized from
344 the first argument to the constructor, if present, or to ``None``, if absent.
345
346
347.. _defaultdict-examples:
348
349:class:`defaultdict` Examples
350^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
351
352Using :class:`list` as the :attr:`default_factory`, it is easy to group a
353sequence of key-value pairs into a dictionary of lists::
354
355 >>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
356 >>> d = defaultdict(list)
357 >>> for k, v in s:
358 ... d[k].append(v)
359 ...
360 >>> d.items()
361 [('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
362
363When each key is encountered for the first time, it is not already in the
364mapping; so an entry is automatically created using the :attr:`default_factory`
365function which returns an empty :class:`list`. The :meth:`list.append`
366operation then attaches the value to the new list. When keys are encountered
367again, the look-up proceeds normally (returning the list for that key) and the
368:meth:`list.append` operation adds another value to the list. This technique is
369simpler and faster than an equivalent technique using :meth:`dict.setdefault`::
370
371 >>> d = {}
372 >>> for k, v in s:
373 ... d.setdefault(k, []).append(v)
374 ...
375 >>> d.items()
376 [('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
377
378Setting the :attr:`default_factory` to :class:`int` makes the
379:class:`defaultdict` useful for counting (like a bag or multiset in other
380languages)::
381
382 >>> s = 'mississippi'
383 >>> d = defaultdict(int)
384 >>> for k in s:
385 ... d[k] += 1
386 ...
387 >>> d.items()
388 [('i', 4), ('p', 2), ('s', 4), ('m', 1)]
389
390When a letter is first encountered, it is missing from the mapping, so the
391:attr:`default_factory` function calls :func:`int` to supply a default count of
392zero. The increment operation then builds up the count for each letter.
393
394The function :func:`int` which always returns zero is just a special case of
395constant functions. A faster and more flexible way to create constant functions
396is to use a lambda function which can supply any constant value (not just
397zero)::
398
399 >>> def constant_factory(value):
400 ... return lambda: value
401 >>> d = defaultdict(constant_factory('<missing>'))
402 >>> d.update(name='John', action='ran')
403 >>> '%(name)s %(action)s to %(object)s' % d
404 'John ran to <missing>'
405
406Setting the :attr:`default_factory` to :class:`set` makes the
407:class:`defaultdict` useful for building a dictionary of sets::
408
409 >>> s = [('red', 1), ('blue', 2), ('red', 3), ('blue', 4), ('red', 1), ('blue', 4)]
410 >>> d = defaultdict(set)
411 >>> for k, v in s:
412 ... d[k].add(v)
413 ...
414 >>> d.items()
415 [('blue', set([2, 4])), ('red', set([1, 3]))]
416
417
418.. _named-tuple-factory:
419
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000420:func:`namedtuple` Factory Function for Tuples with Named Fields
Christian Heimes790c8232008-01-07 21:14:23 +0000421----------------------------------------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000422
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000423Named tuples assign meaning to each position in a tuple and allow for more readable,
424self-documenting code. They can be used wherever regular tuples are used, and
425they add the ability to access fields by name instead of position index.
Georg Brandl116aa622007-08-15 14:28:22 +0000426
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000427.. function:: namedtuple(typename, fieldnames, [verbose])
Georg Brandl116aa622007-08-15 14:28:22 +0000428
429 Returns a new tuple subclass named *typename*. The new subclass is used to
430 create tuple-like objects that have fields accessable by attribute lookup as
431 well as being indexable and iterable. Instances of the subclass also have a
432 helpful docstring (with typename and fieldnames) and a helpful :meth:`__repr__`
433 method which lists the tuple contents in a ``name=value`` format.
434
Georg Brandl9afde1c2007-11-01 20:32:30 +0000435 The *fieldnames* are a single string with each fieldname separated by whitespace
Christian Heimes25bb7832008-01-11 16:17:00 +0000436 and/or commas, for example ``'x y'`` or ``'x, y'``. Alternatively, *fieldnames*
437 can be a sequence of strings such as ``['x', 'y']``.
Georg Brandl9afde1c2007-11-01 20:32:30 +0000438
439 Any valid Python identifier may be used for a fieldname except for names
Christian Heimes0449f632007-12-15 01:27:15 +0000440 starting with an underscore. Valid identifiers consist of letters, digits,
441 and underscores but do not start with a digit or underscore and cannot be
Georg Brandlf6945182008-02-01 11:56:49 +0000442 a :mod:`keyword` such as *class*, *for*, *return*, *global*, *pass*,
Georg Brandl9afde1c2007-11-01 20:32:30 +0000443 or *raise*.
Georg Brandl116aa622007-08-15 14:28:22 +0000444
Christian Heimes25bb7832008-01-11 16:17:00 +0000445 If *verbose* is true, the class definition is printed just before being built.
Georg Brandl116aa622007-08-15 14:28:22 +0000446
Georg Brandl9afde1c2007-11-01 20:32:30 +0000447 Named tuple instances do not have per-instance dictionaries, so they are
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000448 lightweight and require no more memory than regular tuples.
Georg Brandl116aa622007-08-15 14:28:22 +0000449
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000450Example::
Georg Brandl116aa622007-08-15 14:28:22 +0000451
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000452 >>> Point = namedtuple('Point', 'x y', verbose=True)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000453 class Point(tuple):
454 'Point(x, y)'
Christian Heimes0449f632007-12-15 01:27:15 +0000455
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000456 __slots__ = ()
Christian Heimes0449f632007-12-15 01:27:15 +0000457
Christian Heimesfaf2f632008-01-06 16:59:19 +0000458 _fields = ('x', 'y')
459
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000460 def __new__(cls, x, y):
461 return tuple.__new__(cls, (x, y))
Christian Heimes0449f632007-12-15 01:27:15 +0000462
Christian Heimesfaf2f632008-01-06 16:59:19 +0000463 @classmethod
464 def _make(cls, iterable):
465 'Make a new Point object from a sequence or iterable'
466 result = tuple.__new__(cls, iterable)
467 if len(result) != 2:
468 raise TypeError('Expected 2 arguments, got %d' % len(result))
469 return result
Christian Heimes99170a52007-12-19 02:07:34 +0000470
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000471 def __repr__(self):
472 return 'Point(x=%r, y=%r)' % self
Christian Heimes0449f632007-12-15 01:27:15 +0000473
Christian Heimes99170a52007-12-19 02:07:34 +0000474 def _asdict(t):
Christian Heimes0449f632007-12-15 01:27:15 +0000475 'Return a new dict which maps field names to their values'
Christian Heimes99170a52007-12-19 02:07:34 +0000476 return {'x': t[0], 'y': t[1]}
Christian Heimes0449f632007-12-15 01:27:15 +0000477
478 def _replace(self, **kwds):
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000479 'Return a new Point object replacing specified fields with new values'
Christian Heimesfaf2f632008-01-06 16:59:19 +0000480 result = self._make(map(kwds.pop, ('x', 'y'), self))
481 if kwds:
482 raise ValueError('Got unexpected field names: %r' % kwds.keys())
483 return result
Christian Heimes0449f632007-12-15 01:27:15 +0000484
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000485 x = property(itemgetter(0))
486 y = property(itemgetter(1))
Georg Brandl116aa622007-08-15 14:28:22 +0000487
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000488 >>> p = Point(11, y=22) # instantiate with positional or keyword arguments
Christian Heimes99170a52007-12-19 02:07:34 +0000489 >>> p[0] + p[1] # indexable like the plain tuple (11, 22)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000490 33
491 >>> x, y = p # unpack like a regular tuple
492 >>> x, y
493 (11, 22)
494 >>> p.x + p.y # fields also accessable by name
495 33
496 >>> p # readable __repr__ with a name=value style
497 Point(x=11, y=22)
Georg Brandl116aa622007-08-15 14:28:22 +0000498
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000499Named tuples are especially useful for assigning field names to result tuples returned
500by the :mod:`csv` or :mod:`sqlite3` modules::
501
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000502 EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title, department, paygrade')
Georg Brandl9afde1c2007-11-01 20:32:30 +0000503
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000504 import csv
Christian Heimesfaf2f632008-01-06 16:59:19 +0000505 for emp in map(EmployeeRecord._make, csv.reader(open("employees.csv", "rb"))):
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000506 print(emp.name, emp.title)
507
Georg Brandl9afde1c2007-11-01 20:32:30 +0000508 import sqlite3
509 conn = sqlite3.connect('/companydata')
510 cursor = conn.cursor()
511 cursor.execute('SELECT name, age, title, department, paygrade FROM employees')
Christian Heimesfaf2f632008-01-06 16:59:19 +0000512 for emp in map(EmployeeRecord._make, cursor.fetchall()):
Christian Heimes00412232008-01-10 16:02:19 +0000513 print(emp.name, emp.title)
Georg Brandl9afde1c2007-11-01 20:32:30 +0000514
Christian Heimes99170a52007-12-19 02:07:34 +0000515In addition to the methods inherited from tuples, named tuples support
Christian Heimes2380ac72008-01-09 00:17:24 +0000516three additional methods and one attribute. To prevent conflicts with
517field names, the method and attribute names start with an underscore.
Christian Heimes99170a52007-12-19 02:07:34 +0000518
Christian Heimes790c8232008-01-07 21:14:23 +0000519.. method:: somenamedtuple._make(iterable)
Christian Heimes99170a52007-12-19 02:07:34 +0000520
Christian Heimesfaf2f632008-01-06 16:59:19 +0000521 Class method that makes a new instance from an existing sequence or iterable.
Christian Heimes99170a52007-12-19 02:07:34 +0000522
523::
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000524
Christian Heimesfaf2f632008-01-06 16:59:19 +0000525 >>> t = [11, 22]
526 >>> Point._make(t)
527 Point(x=11, y=22)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000528
Christian Heimes790c8232008-01-07 21:14:23 +0000529.. method:: somenamedtuple._asdict()
Georg Brandl9afde1c2007-11-01 20:32:30 +0000530
531 Return a new dict which maps field names to their corresponding values:
532
533::
534
Christian Heimes0449f632007-12-15 01:27:15 +0000535 >>> p._asdict()
Georg Brandl9afde1c2007-11-01 20:32:30 +0000536 {'x': 11, 'y': 22}
537
Christian Heimes790c8232008-01-07 21:14:23 +0000538.. method:: somenamedtuple._replace(kwargs)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000539
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000540 Return a new instance of the named tuple replacing specified fields with new values:
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000541
542::
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000543
544 >>> p = Point(x=11, y=22)
Christian Heimes0449f632007-12-15 01:27:15 +0000545 >>> p._replace(x=33)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000546 Point(x=33, y=22)
547
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000548 >>> for partnum, record in inventory.items():
Christian Heimes454f37b2008-01-10 00:10:02 +0000549 ... inventory[partnum] = record._replace(price=newprices[partnum], timestamp=time.now())
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000550
Christian Heimes790c8232008-01-07 21:14:23 +0000551.. attribute:: somenamedtuple._fields
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000552
Christian Heimes2380ac72008-01-09 00:17:24 +0000553 Tuple of strings listing the field names. Useful for introspection
Georg Brandl9afde1c2007-11-01 20:32:30 +0000554 and for creating new named tuple types from existing named tuples.
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000555
556::
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000557
Christian Heimes0449f632007-12-15 01:27:15 +0000558 >>> p._fields # view the field names
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000559 ('x', 'y')
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000560
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000561 >>> Color = namedtuple('Color', 'red green blue')
Christian Heimes0449f632007-12-15 01:27:15 +0000562 >>> Pixel = namedtuple('Pixel', Point._fields + Color._fields)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000563 >>> Pixel(11, 22, 128, 255, 0)
Christian Heimes454f37b2008-01-10 00:10:02 +0000564 Pixel(x=11, y=22, red=128, green=255, blue=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000565
Christian Heimes0449f632007-12-15 01:27:15 +0000566To retrieve a field whose name is stored in a string, use the :func:`getattr`
Christian Heimes790c8232008-01-07 21:14:23 +0000567function::
Christian Heimes0449f632007-12-15 01:27:15 +0000568
569 >>> getattr(p, 'x')
570 11
571
Christian Heimes25bb7832008-01-11 16:17:00 +0000572To convert a dictionary to a named tuple, use the double-star-operator [#]_::
Christian Heimes99170a52007-12-19 02:07:34 +0000573
574 >>> d = {'x': 11, 'y': 22}
575 >>> Point(**d)
576 Point(x=11, y=22)
577
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000578Since a named tuple is a regular Python class, it is easy to add or change
Christian Heimes043d6f62008-01-07 17:19:16 +0000579functionality with a subclass. Here is how to add a calculated field and
580a fixed-width print format::
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000581
Christian Heimes043d6f62008-01-07 17:19:16 +0000582 >>> class Point(namedtuple('Point', 'x y')):
Christian Heimes25bb7832008-01-11 16:17:00 +0000583 ... __slots__ = ()
Christian Heimes454f37b2008-01-10 00:10:02 +0000584 ... @property
585 ... def hypot(self):
586 ... return (self.x ** 2 + self.y ** 2) ** 0.5
587 ... def __str__(self):
Christian Heimes25bb7832008-01-11 16:17:00 +0000588 ... return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot)
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000589
Christian Heimes25bb7832008-01-11 16:17:00 +0000590 >>> for p in Point(3, 4), Point(14, 5/7.):
Christian Heimes00412232008-01-10 16:02:19 +0000591 ... print(p)
Christian Heimes790c8232008-01-07 21:14:23 +0000592
Christian Heimes25bb7832008-01-11 16:17:00 +0000593 Point: x= 3.000 y= 4.000 hypot= 5.000
594 Point: x=14.000 y= 0.714 hypot=14.018
Christian Heimes043d6f62008-01-07 17:19:16 +0000595
Christian Heimesaf98da12008-01-27 15:18:18 +0000596The subclass shown above sets ``__slots__`` to an empty tuple. This keeps
Christian Heimes679db4a2008-01-18 09:56:22 +0000597keep memory requirements low by preventing the creation of instance dictionaries.
598
Christian Heimes2380ac72008-01-09 00:17:24 +0000599
600Subclassing is not useful for adding new, stored fields. Instead, simply
601create a new named tuple type from the :attr:`_fields` attribute::
602
Christian Heimes25bb7832008-01-11 16:17:00 +0000603 >>> Point3D = namedtuple('Point3D', Point._fields + ('z',))
Christian Heimes2380ac72008-01-09 00:17:24 +0000604
605Default values can be implemented by using :meth:`_replace` to
Christian Heimes790c8232008-01-07 21:14:23 +0000606customize a prototype instance::
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000607
608 >>> Account = namedtuple('Account', 'owner balance transaction_count')
Christian Heimes587c2bf2008-01-19 16:21:02 +0000609 >>> default_account = Account('<owner name>', 0.0, 0)
610 >>> johns_account = default_account._replace(owner='John')
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000611
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000612.. rubric:: Footnotes
613
Christian Heimes99170a52007-12-19 02:07:34 +0000614.. [#] For information on the double-star-operator see
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000615 :ref:`tut-unpacking-arguments` and :ref:`calls`.
Raymond Hettingere4c96ad2008-02-06 01:23:58 +0000616
617
618
619:class:`UserDict` objects
Mark Summerfield8f2d0062008-02-06 13:30:44 +0000620-------------------------
Raymond Hettingere4c96ad2008-02-06 01:23:58 +0000621
622The class, :class:`UserDict` acts as a wrapper around dictionary objects.
623The need for this class has been partially supplanted by the ability to
624subclass directly from :class:`dict`; however, this class can be easier
625to work with because the underlying dictionary is accessible as an
626attribute.
627
628.. class:: UserDict([initialdata])
629
630 Class that simulates a dictionary. The instance's contents are kept in a
631 regular dictionary, which is accessible via the :attr:`data` attribute of
632 :class:`UserDict` instances. If *initialdata* is provided, :attr:`data` is
633 initialized with its contents; note that a reference to *initialdata* will not
634 be kept, allowing it be used for other purposes.
635
636In addition to supporting the methods and operations of mappings,
Raymond Hettingerebcee3f2008-02-06 19:54:00 +0000637:class:`UserDict` instances provide the following attribute:
Raymond Hettingere4c96ad2008-02-06 01:23:58 +0000638
639.. attribute:: UserDict.data
640
641 A real dictionary used to store the contents of the :class:`UserDict` class.