blob: f465c7742bc4d3b0a2fd73049e650de0990fa881 [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
Mark Summerfield8f2d0062008-02-06 13:30:44 +000033===================================== ================================================================================
Mark Summerfield08898b42007-09-05 08:43:04 +000034ABC Notes
Mark Summerfield8f2d0062008-02-06 13:30:44 +000035===================================== ================================================================================
Mark Summerfield08898b42007-09-05 08:43:04 +000036:class:`collections.Container` Defines ``__contains__()``
37:class:`collections.Hashable` Defines ``__hash__()``
38:class:`collections.Iterable` Defines ``__iter__()``
39:class:`collections.Iterator` Derived from :class:`Iterable` and in
40 addition defines ``__next__()``
Raymond Hettingere4c96ad2008-02-06 01:23:58 +000041:class:`collections.Sized` Defines ``__len__()``
Mark Summerfield08898b42007-09-05 08:43:04 +000042:class:`collections.Mapping` Derived from :class:`Container`,
43 :class:`Iterable`,
44 and :class:`Sized`, and in addition
45 defines ``__getitem__()``, ``get()``,
Raymond Hettingere4c96ad2008-02-06 01:23:58 +000046 ``__eq__()``, ``__ne__()``,
Raymond Hettingerc1b6a4a2008-02-08 23:46:23 +000047 ``keys()``, ``items()``, and ``values()``
Mark Summerfield08898b42007-09-05 08:43:04 +000048:class:`collections.MutableMapping` Derived from :class:`Mapping`
Mark Summerfield08898b42007-09-05 08:43:04 +000049:class:`collections.Sequence` Derived from :class:`Container`,
50 :class:`Iterable`, and :class:`Sized`,
51 and in addition defines
Mark Summerfield8f2d0062008-02-06 13:30:44 +000052 ``__getitem__()``
Raymond Hettingere4c96ad2008-02-06 01:23:58 +000053:class:`collections.MutableSequence` Derived from :class:`Sequence`
Mark Summerfield8f2d0062008-02-06 13:30:44 +000054:class:`collections.Set` Derived from :class:`Container`,
55 :class:`Iterable`, and :class:`Sized`,
Raymond Hettingere4c96ad2008-02-06 01:23:58 +000056 add in addition defines
Mark Summerfield8f2d0062008-02-06 13:30:44 +000057 ``__le__()``, ``__lt__()``,
58 ``__eq__()``, ``__and__()``,
59 ``__or__()``, ``__sub__()``,
Raymond Hettingere4c96ad2008-02-06 01:23:58 +000060 ``__xor__()``, and ``isdisjoint()``,
61:class:`collections.MutableSet` Derived from :class:`Set` and in
62 addition defines ``add()``,
63 ``clear()``, ``discard()``, ``pop()``,
Mark Summerfield8f2d0062008-02-06 13:30:44 +000064 ``remove()``, ``__ior__()``,
65 ``__iand__()``, ``__ixor__()``, and
66 ``__isub__()``
67===================================== ================================================================================
Mark Summerfield08898b42007-09-05 08:43:04 +000068
Mark Summerfield08898b42007-09-05 08:43:04 +000069These ABCs allow us to ask classes or instances if they provide
70particular functionality, for example::
71
Mark Summerfield08898b42007-09-05 08:43:04 +000072 size = None
Raymond Hettingerebcee3f2008-02-06 19:54:00 +000073 if isinstance(myvar, collections.Sized):
Mark Summerfield08898b42007-09-05 08:43:04 +000074 size = len(myvar)
75
Raymond Hettingerebcee3f2008-02-06 19:54:00 +000076Several of the ABCs are also useful as mixins that make it easier to develop
77classes supporting container APIs. For example, to write a class supporting
78the full :class:`Set` API, it only necessary to supply the three underlying
79abstract methods: :meth:`__contains__`, :meth:`__iter__`, and :meth:`__len__`.
80The ABC supplies the remaining methods such as :meth:`__and__` and
81:meth:`isdisjoint` ::
82
83 class ListBasedSet(collections.Set):
Raymond Hettingerc1b6a4a2008-02-08 23:46:23 +000084 ''' Alternate set implementation favoring space over speed
85 and not requiring the set elements to be hashable. '''
Raymond Hettingerebcee3f2008-02-06 19:54:00 +000086 def __init__(self, iterable):
Raymond Hettingerc1b6a4a2008-02-08 23:46:23 +000087 self.elements = lst = []
88 for value in iterable:
89 if value not in lst:
90 lst.append(value)
Raymond Hettingerebcee3f2008-02-06 19:54:00 +000091 def __iter__(self):
92 return iter(self.elements)
93 def __contains__(self, value):
94 return value in self.elements
95 def __len__(self):
96 return len(self.elements)
97
98 s1 = ListBasedSet('abcdef')
99 s2 = ListBasedSet('defghi')
100 overlap = s1 & s2 # The __and__() method is supported automatically
101
102
Mark Summerfield08898b42007-09-05 08:43:04 +0000103(For more about ABCs, see the :mod:`abc` module and :pep:`3119`.)
104
105
Georg Brandl116aa622007-08-15 14:28:22 +0000106
107.. _deque-objects:
108
109:class:`deque` objects
110----------------------
111
112
Georg Brandl9afde1c2007-11-01 20:32:30 +0000113.. class:: deque([iterable[, maxlen]])
Georg Brandl116aa622007-08-15 14:28:22 +0000114
115 Returns a new deque object initialized left-to-right (using :meth:`append`) with
116 data from *iterable*. If *iterable* is not specified, the new deque is empty.
117
118 Deques are a generalization of stacks and queues (the name is pronounced "deck"
119 and is short for "double-ended queue"). Deques support thread-safe, memory
120 efficient appends and pops from either side of the deque with approximately the
121 same O(1) performance in either direction.
122
123 Though :class:`list` objects support similar operations, they are optimized for
124 fast fixed-length operations and incur O(n) memory movement costs for
125 ``pop(0)`` and ``insert(0, v)`` operations which change both the size and
126 position of the underlying data representation.
127
Georg Brandl116aa622007-08-15 14:28:22 +0000128
Georg Brandl9afde1c2007-11-01 20:32:30 +0000129 If *maxlen* is not specified or is *None*, deques may grow to an
130 arbitrary length. Otherwise, the deque is bounded to the specified maximum
131 length. Once a bounded length deque is full, when new items are added, a
132 corresponding number of items are discarded from the opposite end. Bounded
133 length deques provide functionality similar to the ``tail`` filter in
134 Unix. They are also useful for tracking transactions and other pools of data
135 where only the most recent activity is of interest.
136
Georg Brandl9afde1c2007-11-01 20:32:30 +0000137
Georg Brandl116aa622007-08-15 14:28:22 +0000138Deque objects support the following methods:
139
Georg Brandl116aa622007-08-15 14:28:22 +0000140.. method:: deque.append(x)
141
142 Add *x* to the right side of the deque.
143
144
145.. method:: deque.appendleft(x)
146
147 Add *x* to the left side of the deque.
148
149
150.. method:: deque.clear()
151
152 Remove all elements from the deque leaving it with length 0.
153
154
155.. method:: deque.extend(iterable)
156
157 Extend the right side of the deque by appending elements from the iterable
158 argument.
159
160
161.. method:: deque.extendleft(iterable)
162
163 Extend the left side of the deque by appending elements from *iterable*. Note,
164 the series of left appends results in reversing the order of elements in the
165 iterable argument.
166
167
168.. method:: deque.pop()
169
170 Remove and return an element from the right side of the deque. If no elements
171 are present, raises an :exc:`IndexError`.
172
173
174.. method:: deque.popleft()
175
176 Remove and return an element from the left side of the deque. If no elements are
177 present, raises an :exc:`IndexError`.
178
179
180.. method:: deque.remove(value)
181
182 Removed the first occurrence of *value*. If not found, raises a
183 :exc:`ValueError`.
184
Georg Brandl116aa622007-08-15 14:28:22 +0000185
186.. method:: deque.rotate(n)
187
188 Rotate the deque *n* steps to the right. If *n* is negative, rotate to the
189 left. Rotating one step to the right is equivalent to:
190 ``d.appendleft(d.pop())``.
191
192In addition to the above, deques support iteration, pickling, ``len(d)``,
193``reversed(d)``, ``copy.copy(d)``, ``copy.deepcopy(d)``, membership testing with
194the :keyword:`in` operator, and subscript references such as ``d[-1]``.
195
196Example::
197
198 >>> from collections import deque
199 >>> d = deque('ghi') # make a new deque with three items
200 >>> for elem in d: # iterate over the deque's elements
Georg Brandl6911e3c2007-09-04 07:15:32 +0000201 ... print(elem.upper())
Georg Brandl116aa622007-08-15 14:28:22 +0000202 G
203 H
204 I
205
206 >>> d.append('j') # add a new entry to the right side
207 >>> d.appendleft('f') # add a new entry to the left side
208 >>> d # show the representation of the deque
209 deque(['f', 'g', 'h', 'i', 'j'])
210
211 >>> d.pop() # return and remove the rightmost item
212 'j'
213 >>> d.popleft() # return and remove the leftmost item
214 'f'
215 >>> list(d) # list the contents of the deque
216 ['g', 'h', 'i']
217 >>> d[0] # peek at leftmost item
218 'g'
219 >>> d[-1] # peek at rightmost item
220 'i'
221
222 >>> list(reversed(d)) # list the contents of a deque in reverse
223 ['i', 'h', 'g']
224 >>> 'h' in d # search the deque
225 True
226 >>> d.extend('jkl') # add multiple elements at once
227 >>> d
228 deque(['g', 'h', 'i', 'j', 'k', 'l'])
229 >>> d.rotate(1) # right rotation
230 >>> d
231 deque(['l', 'g', 'h', 'i', 'j', 'k'])
232 >>> d.rotate(-1) # left rotation
233 >>> d
234 deque(['g', 'h', 'i', 'j', 'k', 'l'])
235
236 >>> deque(reversed(d)) # make a new deque in reverse order
237 deque(['l', 'k', 'j', 'i', 'h', 'g'])
238 >>> d.clear() # empty the deque
239 >>> d.pop() # cannot pop from an empty deque
240 Traceback (most recent call last):
241 File "<pyshell#6>", line 1, in -toplevel-
242 d.pop()
243 IndexError: pop from an empty deque
244
245 >>> d.extendleft('abc') # extendleft() reverses the input order
246 >>> d
247 deque(['c', 'b', 'a'])
248
249
250.. _deque-recipes:
251
Georg Brandl9afde1c2007-11-01 20:32:30 +0000252:class:`deque` Recipes
253^^^^^^^^^^^^^^^^^^^^^^
Georg Brandl116aa622007-08-15 14:28:22 +0000254
255This section shows various approaches to working with deques.
256
257The :meth:`rotate` method provides a way to implement :class:`deque` slicing and
258deletion. For example, a pure python implementation of ``del d[n]`` relies on
259the :meth:`rotate` method to position elements to be popped::
260
261 def delete_nth(d, n):
262 d.rotate(-n)
263 d.popleft()
264 d.rotate(n)
265
266To implement :class:`deque` slicing, use a similar approach applying
267:meth:`rotate` to bring a target element to the left side of the deque. Remove
268old entries with :meth:`popleft`, add new entries with :meth:`extend`, and then
269reverse the rotation.
Georg Brandl116aa622007-08-15 14:28:22 +0000270With minor variations on that approach, it is easy to implement Forth style
271stack manipulations such as ``dup``, ``drop``, ``swap``, ``over``, ``pick``,
272``rot``, and ``roll``.
273
Georg Brandl116aa622007-08-15 14:28:22 +0000274Multi-pass data reduction algorithms can be succinctly expressed and efficiently
275coded by extracting elements with multiple calls to :meth:`popleft`, applying
Georg Brandl9afde1c2007-11-01 20:32:30 +0000276a reduction function, and calling :meth:`append` to add the result back to the
277deque.
Georg Brandl116aa622007-08-15 14:28:22 +0000278
279For example, building a balanced binary tree of nested lists entails reducing
280two adjacent nodes into one by grouping them in a list::
281
282 >>> def maketree(iterable):
283 ... d = deque(iterable)
284 ... while len(d) > 1:
285 ... pair = [d.popleft(), d.popleft()]
286 ... d.append(pair)
287 ... return list(d)
288 ...
Georg Brandl6911e3c2007-09-04 07:15:32 +0000289 >>> print(maketree('abcdefgh'))
Georg Brandl116aa622007-08-15 14:28:22 +0000290 [[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]]
291
Georg Brandl9afde1c2007-11-01 20:32:30 +0000292Bounded length deques provide functionality similar to the ``tail`` filter
293in Unix::
Georg Brandl116aa622007-08-15 14:28:22 +0000294
Georg Brandl9afde1c2007-11-01 20:32:30 +0000295 def tail(filename, n=10):
296 'Return the last n lines of a file'
297 return deque(open(filename), n)
Georg Brandl116aa622007-08-15 14:28:22 +0000298
299.. _defaultdict-objects:
300
301:class:`defaultdict` objects
302----------------------------
303
304
305.. class:: defaultdict([default_factory[, ...]])
306
307 Returns a new dictionary-like object. :class:`defaultdict` is a subclass of the
308 builtin :class:`dict` class. It overrides one method and adds one writable
309 instance variable. The remaining functionality is the same as for the
310 :class:`dict` class and is not documented here.
311
312 The first argument provides the initial value for the :attr:`default_factory`
313 attribute; it defaults to ``None``. All remaining arguments are treated the same
314 as if they were passed to the :class:`dict` constructor, including keyword
315 arguments.
316
Georg Brandl116aa622007-08-15 14:28:22 +0000317
318:class:`defaultdict` objects support the following method in addition to the
319standard :class:`dict` operations:
320
Georg Brandl116aa622007-08-15 14:28:22 +0000321.. method:: defaultdict.__missing__(key)
322
323 If the :attr:`default_factory` attribute is ``None``, this raises an
324 :exc:`KeyError` exception with the *key* as argument.
325
326 If :attr:`default_factory` is not ``None``, it is called without arguments to
327 provide a default value for the given *key*, this value is inserted in the
328 dictionary for the *key*, and returned.
329
330 If calling :attr:`default_factory` raises an exception this exception is
331 propagated unchanged.
332
333 This method is called by the :meth:`__getitem__` method of the :class:`dict`
334 class when the requested key is not found; whatever it returns or raises is then
335 returned or raised by :meth:`__getitem__`.
336
337:class:`defaultdict` objects support the following instance variable:
338
339
340.. attribute:: defaultdict.default_factory
341
342 This attribute is used by the :meth:`__missing__` method; it is initialized from
343 the first argument to the constructor, if present, or to ``None``, if absent.
344
345
346.. _defaultdict-examples:
347
348:class:`defaultdict` Examples
349^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
350
351Using :class:`list` as the :attr:`default_factory`, it is easy to group a
352sequence of key-value pairs into a dictionary of lists::
353
354 >>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
355 >>> d = defaultdict(list)
356 >>> for k, v in s:
357 ... d[k].append(v)
358 ...
359 >>> d.items()
360 [('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
361
362When each key is encountered for the first time, it is not already in the
363mapping; so an entry is automatically created using the :attr:`default_factory`
364function which returns an empty :class:`list`. The :meth:`list.append`
365operation then attaches the value to the new list. When keys are encountered
366again, the look-up proceeds normally (returning the list for that key) and the
367:meth:`list.append` operation adds another value to the list. This technique is
368simpler and faster than an equivalent technique using :meth:`dict.setdefault`::
369
370 >>> d = {}
371 >>> for k, v in s:
372 ... d.setdefault(k, []).append(v)
373 ...
374 >>> d.items()
375 [('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
376
377Setting the :attr:`default_factory` to :class:`int` makes the
378:class:`defaultdict` useful for counting (like a bag or multiset in other
379languages)::
380
381 >>> s = 'mississippi'
382 >>> d = defaultdict(int)
383 >>> for k in s:
384 ... d[k] += 1
385 ...
386 >>> d.items()
387 [('i', 4), ('p', 2), ('s', 4), ('m', 1)]
388
389When a letter is first encountered, it is missing from the mapping, so the
390:attr:`default_factory` function calls :func:`int` to supply a default count of
391zero. The increment operation then builds up the count for each letter.
392
393The function :func:`int` which always returns zero is just a special case of
394constant functions. A faster and more flexible way to create constant functions
395is to use a lambda function which can supply any constant value (not just
396zero)::
397
398 >>> def constant_factory(value):
399 ... return lambda: value
400 >>> d = defaultdict(constant_factory('<missing>'))
401 >>> d.update(name='John', action='ran')
402 >>> '%(name)s %(action)s to %(object)s' % d
403 'John ran to <missing>'
404
405Setting the :attr:`default_factory` to :class:`set` makes the
406:class:`defaultdict` useful for building a dictionary of sets::
407
408 >>> s = [('red', 1), ('blue', 2), ('red', 3), ('blue', 4), ('red', 1), ('blue', 4)]
409 >>> d = defaultdict(set)
410 >>> for k, v in s:
411 ... d[k].add(v)
412 ...
413 >>> d.items()
414 [('blue', set([2, 4])), ('red', set([1, 3]))]
415
416
417.. _named-tuple-factory:
418
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000419:func:`namedtuple` Factory Function for Tuples with Named Fields
Christian Heimes790c8232008-01-07 21:14:23 +0000420----------------------------------------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000421
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000422Named tuples assign meaning to each position in a tuple and allow for more readable,
423self-documenting code. They can be used wherever regular tuples are used, and
424they add the ability to access fields by name instead of position index.
Georg Brandl116aa622007-08-15 14:28:22 +0000425
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000426.. function:: namedtuple(typename, fieldnames, [verbose])
Georg Brandl116aa622007-08-15 14:28:22 +0000427
428 Returns a new tuple subclass named *typename*. The new subclass is used to
429 create tuple-like objects that have fields accessable by attribute lookup as
430 well as being indexable and iterable. Instances of the subclass also have a
431 helpful docstring (with typename and fieldnames) and a helpful :meth:`__repr__`
432 method which lists the tuple contents in a ``name=value`` format.
433
Georg Brandl9afde1c2007-11-01 20:32:30 +0000434 The *fieldnames* are a single string with each fieldname separated by whitespace
Christian Heimes25bb7832008-01-11 16:17:00 +0000435 and/or commas, for example ``'x y'`` or ``'x, y'``. Alternatively, *fieldnames*
436 can be a sequence of strings such as ``['x', 'y']``.
Georg Brandl9afde1c2007-11-01 20:32:30 +0000437
438 Any valid Python identifier may be used for a fieldname except for names
Christian Heimes0449f632007-12-15 01:27:15 +0000439 starting with an underscore. Valid identifiers consist of letters, digits,
440 and underscores but do not start with a digit or underscore and cannot be
Georg Brandlf6945182008-02-01 11:56:49 +0000441 a :mod:`keyword` such as *class*, *for*, *return*, *global*, *pass*,
Georg Brandl9afde1c2007-11-01 20:32:30 +0000442 or *raise*.
Georg Brandl116aa622007-08-15 14:28:22 +0000443
Christian Heimes25bb7832008-01-11 16:17:00 +0000444 If *verbose* is true, the class definition is printed just before being built.
Georg Brandl116aa622007-08-15 14:28:22 +0000445
Georg Brandl9afde1c2007-11-01 20:32:30 +0000446 Named tuple instances do not have per-instance dictionaries, so they are
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000447 lightweight and require no more memory than regular tuples.
Georg Brandl116aa622007-08-15 14:28:22 +0000448
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000449Example::
Georg Brandl116aa622007-08-15 14:28:22 +0000450
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000451 >>> Point = namedtuple('Point', 'x y', verbose=True)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000452 class Point(tuple):
453 'Point(x, y)'
Christian Heimes0449f632007-12-15 01:27:15 +0000454
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000455 __slots__ = ()
Christian Heimes0449f632007-12-15 01:27:15 +0000456
Christian Heimesfaf2f632008-01-06 16:59:19 +0000457 _fields = ('x', 'y')
458
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000459 def __new__(cls, x, y):
460 return tuple.__new__(cls, (x, y))
Christian Heimes0449f632007-12-15 01:27:15 +0000461
Christian Heimesfaf2f632008-01-06 16:59:19 +0000462 @classmethod
463 def _make(cls, iterable):
464 'Make a new Point object from a sequence or iterable'
465 result = tuple.__new__(cls, iterable)
466 if len(result) != 2:
467 raise TypeError('Expected 2 arguments, got %d' % len(result))
468 return result
Christian Heimes99170a52007-12-19 02:07:34 +0000469
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000470 def __repr__(self):
471 return 'Point(x=%r, y=%r)' % self
Christian Heimes0449f632007-12-15 01:27:15 +0000472
Christian Heimes99170a52007-12-19 02:07:34 +0000473 def _asdict(t):
Christian Heimes0449f632007-12-15 01:27:15 +0000474 'Return a new dict which maps field names to their values'
Christian Heimes99170a52007-12-19 02:07:34 +0000475 return {'x': t[0], 'y': t[1]}
Christian Heimes0449f632007-12-15 01:27:15 +0000476
477 def _replace(self, **kwds):
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000478 'Return a new Point object replacing specified fields with new values'
Christian Heimesfaf2f632008-01-06 16:59:19 +0000479 result = self._make(map(kwds.pop, ('x', 'y'), self))
480 if kwds:
481 raise ValueError('Got unexpected field names: %r' % kwds.keys())
482 return result
Christian Heimes0449f632007-12-15 01:27:15 +0000483
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000484 x = property(itemgetter(0))
485 y = property(itemgetter(1))
Georg Brandl116aa622007-08-15 14:28:22 +0000486
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000487 >>> p = Point(11, y=22) # instantiate with positional or keyword arguments
Christian Heimes99170a52007-12-19 02:07:34 +0000488 >>> p[0] + p[1] # indexable like the plain tuple (11, 22)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000489 33
490 >>> x, y = p # unpack like a regular tuple
491 >>> x, y
492 (11, 22)
493 >>> p.x + p.y # fields also accessable by name
494 33
495 >>> p # readable __repr__ with a name=value style
496 Point(x=11, y=22)
Georg Brandl116aa622007-08-15 14:28:22 +0000497
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000498Named tuples are especially useful for assigning field names to result tuples returned
499by the :mod:`csv` or :mod:`sqlite3` modules::
500
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000501 EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title, department, paygrade')
Georg Brandl9afde1c2007-11-01 20:32:30 +0000502
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000503 import csv
Christian Heimesfaf2f632008-01-06 16:59:19 +0000504 for emp in map(EmployeeRecord._make, csv.reader(open("employees.csv", "rb"))):
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000505 print(emp.name, emp.title)
506
Georg Brandl9afde1c2007-11-01 20:32:30 +0000507 import sqlite3
508 conn = sqlite3.connect('/companydata')
509 cursor = conn.cursor()
510 cursor.execute('SELECT name, age, title, department, paygrade FROM employees')
Christian Heimesfaf2f632008-01-06 16:59:19 +0000511 for emp in map(EmployeeRecord._make, cursor.fetchall()):
Christian Heimes00412232008-01-10 16:02:19 +0000512 print(emp.name, emp.title)
Georg Brandl9afde1c2007-11-01 20:32:30 +0000513
Christian Heimes99170a52007-12-19 02:07:34 +0000514In addition to the methods inherited from tuples, named tuples support
Christian Heimes2380ac72008-01-09 00:17:24 +0000515three additional methods and one attribute. To prevent conflicts with
516field names, the method and attribute names start with an underscore.
Christian Heimes99170a52007-12-19 02:07:34 +0000517
Christian Heimes790c8232008-01-07 21:14:23 +0000518.. method:: somenamedtuple._make(iterable)
Christian Heimes99170a52007-12-19 02:07:34 +0000519
Christian Heimesfaf2f632008-01-06 16:59:19 +0000520 Class method that makes a new instance from an existing sequence or iterable.
Christian Heimes99170a52007-12-19 02:07:34 +0000521
522::
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000523
Christian Heimesfaf2f632008-01-06 16:59:19 +0000524 >>> t = [11, 22]
525 >>> Point._make(t)
526 Point(x=11, y=22)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000527
Christian Heimes790c8232008-01-07 21:14:23 +0000528.. method:: somenamedtuple._asdict()
Georg Brandl9afde1c2007-11-01 20:32:30 +0000529
530 Return a new dict which maps field names to their corresponding values:
531
532::
533
Christian Heimes0449f632007-12-15 01:27:15 +0000534 >>> p._asdict()
Georg Brandl9afde1c2007-11-01 20:32:30 +0000535 {'x': 11, 'y': 22}
536
Christian Heimes790c8232008-01-07 21:14:23 +0000537.. method:: somenamedtuple._replace(kwargs)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000538
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000539 Return a new instance of the named tuple replacing specified fields with new values:
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000540
541::
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000542
543 >>> p = Point(x=11, y=22)
Christian Heimes0449f632007-12-15 01:27:15 +0000544 >>> p._replace(x=33)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000545 Point(x=33, y=22)
546
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000547 >>> for partnum, record in inventory.items():
Christian Heimes454f37b2008-01-10 00:10:02 +0000548 ... inventory[partnum] = record._replace(price=newprices[partnum], timestamp=time.now())
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000549
Christian Heimes790c8232008-01-07 21:14:23 +0000550.. attribute:: somenamedtuple._fields
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000551
Christian Heimes2380ac72008-01-09 00:17:24 +0000552 Tuple of strings listing the field names. Useful for introspection
Georg Brandl9afde1c2007-11-01 20:32:30 +0000553 and for creating new named tuple types from existing named tuples.
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000554
555::
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000556
Christian Heimes0449f632007-12-15 01:27:15 +0000557 >>> p._fields # view the field names
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000558 ('x', 'y')
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000559
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000560 >>> Color = namedtuple('Color', 'red green blue')
Christian Heimes0449f632007-12-15 01:27:15 +0000561 >>> Pixel = namedtuple('Pixel', Point._fields + Color._fields)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000562 >>> Pixel(11, 22, 128, 255, 0)
Christian Heimes454f37b2008-01-10 00:10:02 +0000563 Pixel(x=11, y=22, red=128, green=255, blue=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000564
Christian Heimes0449f632007-12-15 01:27:15 +0000565To retrieve a field whose name is stored in a string, use the :func:`getattr`
Christian Heimes790c8232008-01-07 21:14:23 +0000566function::
Christian Heimes0449f632007-12-15 01:27:15 +0000567
568 >>> getattr(p, 'x')
569 11
570
Christian Heimes25bb7832008-01-11 16:17:00 +0000571To convert a dictionary to a named tuple, use the double-star-operator [#]_::
Christian Heimes99170a52007-12-19 02:07:34 +0000572
573 >>> d = {'x': 11, 'y': 22}
574 >>> Point(**d)
575 Point(x=11, y=22)
576
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000577Since a named tuple is a regular Python class, it is easy to add or change
Christian Heimes043d6f62008-01-07 17:19:16 +0000578functionality with a subclass. Here is how to add a calculated field and
579a fixed-width print format::
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000580
Christian Heimes043d6f62008-01-07 17:19:16 +0000581 >>> class Point(namedtuple('Point', 'x y')):
Christian Heimes25bb7832008-01-11 16:17:00 +0000582 ... __slots__ = ()
Christian Heimes454f37b2008-01-10 00:10:02 +0000583 ... @property
584 ... def hypot(self):
585 ... return (self.x ** 2 + self.y ** 2) ** 0.5
586 ... def __str__(self):
Christian Heimes25bb7832008-01-11 16:17:00 +0000587 ... 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 +0000588
Christian Heimes25bb7832008-01-11 16:17:00 +0000589 >>> for p in Point(3, 4), Point(14, 5/7.):
Christian Heimes00412232008-01-10 16:02:19 +0000590 ... print(p)
Christian Heimes790c8232008-01-07 21:14:23 +0000591
Christian Heimes25bb7832008-01-11 16:17:00 +0000592 Point: x= 3.000 y= 4.000 hypot= 5.000
593 Point: x=14.000 y= 0.714 hypot=14.018
Christian Heimes043d6f62008-01-07 17:19:16 +0000594
Christian Heimesaf98da12008-01-27 15:18:18 +0000595The subclass shown above sets ``__slots__`` to an empty tuple. This keeps
Christian Heimes679db4a2008-01-18 09:56:22 +0000596keep memory requirements low by preventing the creation of instance dictionaries.
597
Christian Heimes2380ac72008-01-09 00:17:24 +0000598
599Subclassing is not useful for adding new, stored fields. Instead, simply
600create a new named tuple type from the :attr:`_fields` attribute::
601
Christian Heimes25bb7832008-01-11 16:17:00 +0000602 >>> Point3D = namedtuple('Point3D', Point._fields + ('z',))
Christian Heimes2380ac72008-01-09 00:17:24 +0000603
604Default values can be implemented by using :meth:`_replace` to
Christian Heimes790c8232008-01-07 21:14:23 +0000605customize a prototype instance::
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000606
607 >>> Account = namedtuple('Account', 'owner balance transaction_count')
Christian Heimes587c2bf2008-01-19 16:21:02 +0000608 >>> default_account = Account('<owner name>', 0.0, 0)
609 >>> johns_account = default_account._replace(owner='John')
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000610
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000611.. rubric:: Footnotes
612
Christian Heimes99170a52007-12-19 02:07:34 +0000613.. [#] For information on the double-star-operator see
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000614 :ref:`tut-unpacking-arguments` and :ref:`calls`.
Raymond Hettingere4c96ad2008-02-06 01:23:58 +0000615
616
617
618:class:`UserDict` objects
Mark Summerfield8f2d0062008-02-06 13:30:44 +0000619-------------------------
Raymond Hettingere4c96ad2008-02-06 01:23:58 +0000620
621The class, :class:`UserDict` acts as a wrapper around dictionary objects.
622The need for this class has been partially supplanted by the ability to
623subclass directly from :class:`dict`; however, this class can be easier
624to work with because the underlying dictionary is accessible as an
625attribute.
626
627.. class:: UserDict([initialdata])
628
629 Class that simulates a dictionary. The instance's contents are kept in a
630 regular dictionary, which is accessible via the :attr:`data` attribute of
631 :class:`UserDict` instances. If *initialdata* is provided, :attr:`data` is
632 initialized with its contents; note that a reference to *initialdata* will not
633 be kept, allowing it be used for other purposes.
634
635In addition to supporting the methods and operations of mappings,
Raymond Hettingerebcee3f2008-02-06 19:54:00 +0000636:class:`UserDict` instances provide the following attribute:
Raymond Hettingere4c96ad2008-02-06 01:23:58 +0000637
638.. attribute:: UserDict.data
639
640 A real dictionary used to store the contents of the :class:`UserDict` class.