blob: f70ccb699de4fd2e678640e6be1f8f1f491b0d5b [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
Raymond Hettinger53dbe392008-02-12 20:03:09 +00002:mod:`collections` --- Container datatypes
3==========================================
Georg Brandl116aa622007-08-15 14:28:22 +00004
5.. module:: collections
Raymond Hettinger53dbe392008-02-12 20:03:09 +00006 :synopsis: Container datatypes
Georg Brandl116aa622007-08-15 14:28:22 +00007.. 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
Raymond Hettinger0dbdab22008-02-09 03:48:16 +000061:class:`Set` :class:`Sized`, ``__len__``, ``__le__``, ``__lt__``, ``__eq__``, ``__ne__``,
Raymond Hettinger409fb2c2008-02-09 02:17:06 +000062 :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
Raymond Hettinger7aebb642008-02-09 03:25:08 +0000103Notes on using :class:`Set` and :class:`MutableSet` as a mixin:
104
105(1)
106 Since some set operations create new sets, the default mixin methods need
107 a way to create new instances from an iterable. The class constructor is
108 assumed to have a signature in the form ``ClassName(iterable)``.
109 That assumption is factored-out to a singleinternal classmethod called
110 :meth:`_from_iterable` which calls ``cls(iterable)`` to produce a new set.
111 If the :class:`Set` mixin is being used in a class with a different
112 constructor signature, you will need to override :meth:`from_iterable`
113 with a classmethod that can construct new instances from
114 an iterable argument.
115
116(2)
117 To override the comparisons (presumably for speed, as the
118 semantics are fixed), redefine :meth:`__le__` and
119 then the other operations will automatically follow suit.
Raymond Hettingerebcee3f2008-02-06 19:54:00 +0000120
Raymond Hettinger0dbdab22008-02-09 03:48:16 +0000121(3)
122 The :class:`Set` mixin provides a :meth:`_hash` method to compute a hash value
123 for the set; however, :meth:`__hash__` is not defined because not all sets
124 are hashable or immutable. To add set hashabilty using mixins,
125 inherit from both :meth:`Set` and :meth:`Hashable`, then define
126 ``__hash__ = Set._hash``.
127
Mark Summerfield08898b42007-09-05 08:43:04 +0000128(For more about ABCs, see the :mod:`abc` module and :pep:`3119`.)
129
130
Georg Brandl116aa622007-08-15 14:28:22 +0000131
132.. _deque-objects:
133
134:class:`deque` objects
135----------------------
136
137
Georg Brandl9afde1c2007-11-01 20:32:30 +0000138.. class:: deque([iterable[, maxlen]])
Georg Brandl116aa622007-08-15 14:28:22 +0000139
140 Returns a new deque object initialized left-to-right (using :meth:`append`) with
141 data from *iterable*. If *iterable* is not specified, the new deque is empty.
142
143 Deques are a generalization of stacks and queues (the name is pronounced "deck"
144 and is short for "double-ended queue"). Deques support thread-safe, memory
145 efficient appends and pops from either side of the deque with approximately the
146 same O(1) performance in either direction.
147
148 Though :class:`list` objects support similar operations, they are optimized for
149 fast fixed-length operations and incur O(n) memory movement costs for
150 ``pop(0)`` and ``insert(0, v)`` operations which change both the size and
151 position of the underlying data representation.
152
Georg Brandl116aa622007-08-15 14:28:22 +0000153
Georg Brandl9afde1c2007-11-01 20:32:30 +0000154 If *maxlen* is not specified or is *None*, deques may grow to an
155 arbitrary length. Otherwise, the deque is bounded to the specified maximum
156 length. Once a bounded length deque is full, when new items are added, a
157 corresponding number of items are discarded from the opposite end. Bounded
158 length deques provide functionality similar to the ``tail`` filter in
159 Unix. They are also useful for tracking transactions and other pools of data
160 where only the most recent activity is of interest.
161
Georg Brandl9afde1c2007-11-01 20:32:30 +0000162
Georg Brandl116aa622007-08-15 14:28:22 +0000163Deque objects support the following methods:
164
Georg Brandl116aa622007-08-15 14:28:22 +0000165.. method:: deque.append(x)
166
167 Add *x* to the right side of the deque.
168
169
170.. method:: deque.appendleft(x)
171
172 Add *x* to the left side of the deque.
173
174
175.. method:: deque.clear()
176
177 Remove all elements from the deque leaving it with length 0.
178
179
180.. method:: deque.extend(iterable)
181
182 Extend the right side of the deque by appending elements from the iterable
183 argument.
184
185
186.. method:: deque.extendleft(iterable)
187
188 Extend the left side of the deque by appending elements from *iterable*. Note,
189 the series of left appends results in reversing the order of elements in the
190 iterable argument.
191
192
193.. method:: deque.pop()
194
195 Remove and return an element from the right side of the deque. If no elements
196 are present, raises an :exc:`IndexError`.
197
198
199.. method:: deque.popleft()
200
201 Remove and return an element from the left side of the deque. If no elements are
202 present, raises an :exc:`IndexError`.
203
204
205.. method:: deque.remove(value)
206
207 Removed the first occurrence of *value*. If not found, raises a
208 :exc:`ValueError`.
209
Georg Brandl116aa622007-08-15 14:28:22 +0000210
211.. method:: deque.rotate(n)
212
213 Rotate the deque *n* steps to the right. If *n* is negative, rotate to the
214 left. Rotating one step to the right is equivalent to:
215 ``d.appendleft(d.pop())``.
216
217In addition to the above, deques support iteration, pickling, ``len(d)``,
218``reversed(d)``, ``copy.copy(d)``, ``copy.deepcopy(d)``, membership testing with
219the :keyword:`in` operator, and subscript references such as ``d[-1]``.
220
221Example::
222
223 >>> from collections import deque
224 >>> d = deque('ghi') # make a new deque with three items
225 >>> for elem in d: # iterate over the deque's elements
Georg Brandl6911e3c2007-09-04 07:15:32 +0000226 ... print(elem.upper())
Georg Brandl116aa622007-08-15 14:28:22 +0000227 G
228 H
229 I
230
231 >>> d.append('j') # add a new entry to the right side
232 >>> d.appendleft('f') # add a new entry to the left side
233 >>> d # show the representation of the deque
234 deque(['f', 'g', 'h', 'i', 'j'])
235
236 >>> d.pop() # return and remove the rightmost item
237 'j'
238 >>> d.popleft() # return and remove the leftmost item
239 'f'
240 >>> list(d) # list the contents of the deque
241 ['g', 'h', 'i']
242 >>> d[0] # peek at leftmost item
243 'g'
244 >>> d[-1] # peek at rightmost item
245 'i'
246
247 >>> list(reversed(d)) # list the contents of a deque in reverse
248 ['i', 'h', 'g']
249 >>> 'h' in d # search the deque
250 True
251 >>> d.extend('jkl') # add multiple elements at once
252 >>> d
253 deque(['g', 'h', 'i', 'j', 'k', 'l'])
254 >>> d.rotate(1) # right rotation
255 >>> d
256 deque(['l', 'g', 'h', 'i', 'j', 'k'])
257 >>> d.rotate(-1) # left rotation
258 >>> d
259 deque(['g', 'h', 'i', 'j', 'k', 'l'])
260
261 >>> deque(reversed(d)) # make a new deque in reverse order
262 deque(['l', 'k', 'j', 'i', 'h', 'g'])
263 >>> d.clear() # empty the deque
264 >>> d.pop() # cannot pop from an empty deque
265 Traceback (most recent call last):
266 File "<pyshell#6>", line 1, in -toplevel-
267 d.pop()
268 IndexError: pop from an empty deque
269
270 >>> d.extendleft('abc') # extendleft() reverses the input order
271 >>> d
272 deque(['c', 'b', 'a'])
273
274
275.. _deque-recipes:
276
Georg Brandl9afde1c2007-11-01 20:32:30 +0000277:class:`deque` Recipes
278^^^^^^^^^^^^^^^^^^^^^^
Georg Brandl116aa622007-08-15 14:28:22 +0000279
280This section shows various approaches to working with deques.
281
282The :meth:`rotate` method provides a way to implement :class:`deque` slicing and
283deletion. For example, a pure python implementation of ``del d[n]`` relies on
284the :meth:`rotate` method to position elements to be popped::
285
286 def delete_nth(d, n):
287 d.rotate(-n)
288 d.popleft()
289 d.rotate(n)
290
291To implement :class:`deque` slicing, use a similar approach applying
292:meth:`rotate` to bring a target element to the left side of the deque. Remove
293old entries with :meth:`popleft`, add new entries with :meth:`extend`, and then
294reverse the rotation.
Georg Brandl116aa622007-08-15 14:28:22 +0000295With minor variations on that approach, it is easy to implement Forth style
296stack manipulations such as ``dup``, ``drop``, ``swap``, ``over``, ``pick``,
297``rot``, and ``roll``.
298
Georg Brandl116aa622007-08-15 14:28:22 +0000299Multi-pass data reduction algorithms can be succinctly expressed and efficiently
300coded by extracting elements with multiple calls to :meth:`popleft`, applying
Georg Brandl9afde1c2007-11-01 20:32:30 +0000301a reduction function, and calling :meth:`append` to add the result back to the
302deque.
Georg Brandl116aa622007-08-15 14:28:22 +0000303
304For example, building a balanced binary tree of nested lists entails reducing
305two adjacent nodes into one by grouping them in a list::
306
307 >>> def maketree(iterable):
308 ... d = deque(iterable)
309 ... while len(d) > 1:
310 ... pair = [d.popleft(), d.popleft()]
311 ... d.append(pair)
312 ... return list(d)
313 ...
Georg Brandl6911e3c2007-09-04 07:15:32 +0000314 >>> print(maketree('abcdefgh'))
Georg Brandl116aa622007-08-15 14:28:22 +0000315 [[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]]
316
Georg Brandl9afde1c2007-11-01 20:32:30 +0000317Bounded length deques provide functionality similar to the ``tail`` filter
318in Unix::
Georg Brandl116aa622007-08-15 14:28:22 +0000319
Georg Brandl9afde1c2007-11-01 20:32:30 +0000320 def tail(filename, n=10):
321 'Return the last n lines of a file'
322 return deque(open(filename), n)
Georg Brandl116aa622007-08-15 14:28:22 +0000323
324.. _defaultdict-objects:
325
326:class:`defaultdict` objects
327----------------------------
328
329
330.. class:: defaultdict([default_factory[, ...]])
331
332 Returns a new dictionary-like object. :class:`defaultdict` is a subclass of the
333 builtin :class:`dict` class. It overrides one method and adds one writable
334 instance variable. The remaining functionality is the same as for the
335 :class:`dict` class and is not documented here.
336
337 The first argument provides the initial value for the :attr:`default_factory`
338 attribute; it defaults to ``None``. All remaining arguments are treated the same
339 as if they were passed to the :class:`dict` constructor, including keyword
340 arguments.
341
Georg Brandl116aa622007-08-15 14:28:22 +0000342
343:class:`defaultdict` objects support the following method in addition to the
344standard :class:`dict` operations:
345
Georg Brandl116aa622007-08-15 14:28:22 +0000346.. method:: defaultdict.__missing__(key)
347
348 If the :attr:`default_factory` attribute is ``None``, this raises an
349 :exc:`KeyError` exception with the *key* as argument.
350
351 If :attr:`default_factory` is not ``None``, it is called without arguments to
352 provide a default value for the given *key*, this value is inserted in the
353 dictionary for the *key*, and returned.
354
355 If calling :attr:`default_factory` raises an exception this exception is
356 propagated unchanged.
357
358 This method is called by the :meth:`__getitem__` method of the :class:`dict`
359 class when the requested key is not found; whatever it returns or raises is then
360 returned or raised by :meth:`__getitem__`.
361
362:class:`defaultdict` objects support the following instance variable:
363
364
365.. attribute:: defaultdict.default_factory
366
367 This attribute is used by the :meth:`__missing__` method; it is initialized from
368 the first argument to the constructor, if present, or to ``None``, if absent.
369
370
371.. _defaultdict-examples:
372
373:class:`defaultdict` Examples
374^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
375
376Using :class:`list` as the :attr:`default_factory`, it is easy to group a
377sequence of key-value pairs into a dictionary of lists::
378
379 >>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
380 >>> d = defaultdict(list)
381 >>> for k, v in s:
382 ... d[k].append(v)
383 ...
384 >>> d.items()
385 [('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
386
387When each key is encountered for the first time, it is not already in the
388mapping; so an entry is automatically created using the :attr:`default_factory`
389function which returns an empty :class:`list`. The :meth:`list.append`
390operation then attaches the value to the new list. When keys are encountered
391again, the look-up proceeds normally (returning the list for that key) and the
392:meth:`list.append` operation adds another value to the list. This technique is
393simpler and faster than an equivalent technique using :meth:`dict.setdefault`::
394
395 >>> d = {}
396 >>> for k, v in s:
397 ... d.setdefault(k, []).append(v)
398 ...
399 >>> d.items()
400 [('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
401
402Setting the :attr:`default_factory` to :class:`int` makes the
403:class:`defaultdict` useful for counting (like a bag or multiset in other
404languages)::
405
406 >>> s = 'mississippi'
407 >>> d = defaultdict(int)
408 >>> for k in s:
409 ... d[k] += 1
410 ...
411 >>> d.items()
412 [('i', 4), ('p', 2), ('s', 4), ('m', 1)]
413
414When a letter is first encountered, it is missing from the mapping, so the
415:attr:`default_factory` function calls :func:`int` to supply a default count of
416zero. The increment operation then builds up the count for each letter.
417
418The function :func:`int` which always returns zero is just a special case of
419constant functions. A faster and more flexible way to create constant functions
420is to use a lambda function which can supply any constant value (not just
421zero)::
422
423 >>> def constant_factory(value):
424 ... return lambda: value
425 >>> d = defaultdict(constant_factory('<missing>'))
426 >>> d.update(name='John', action='ran')
427 >>> '%(name)s %(action)s to %(object)s' % d
428 'John ran to <missing>'
429
430Setting the :attr:`default_factory` to :class:`set` makes the
431:class:`defaultdict` useful for building a dictionary of sets::
432
433 >>> s = [('red', 1), ('blue', 2), ('red', 3), ('blue', 4), ('red', 1), ('blue', 4)]
434 >>> d = defaultdict(set)
435 >>> for k, v in s:
436 ... d[k].add(v)
437 ...
438 >>> d.items()
439 [('blue', set([2, 4])), ('red', set([1, 3]))]
440
441
442.. _named-tuple-factory:
443
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000444:func:`namedtuple` Factory Function for Tuples with Named Fields
Christian Heimes790c8232008-01-07 21:14:23 +0000445----------------------------------------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000446
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000447Named tuples assign meaning to each position in a tuple and allow for more readable,
448self-documenting code. They can be used wherever regular tuples are used, and
449they add the ability to access fields by name instead of position index.
Georg Brandl116aa622007-08-15 14:28:22 +0000450
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000451.. function:: namedtuple(typename, fieldnames, [verbose])
Georg Brandl116aa622007-08-15 14:28:22 +0000452
453 Returns a new tuple subclass named *typename*. The new subclass is used to
454 create tuple-like objects that have fields accessable by attribute lookup as
455 well as being indexable and iterable. Instances of the subclass also have a
456 helpful docstring (with typename and fieldnames) and a helpful :meth:`__repr__`
457 method which lists the tuple contents in a ``name=value`` format.
458
Georg Brandl9afde1c2007-11-01 20:32:30 +0000459 The *fieldnames* are a single string with each fieldname separated by whitespace
Christian Heimes25bb7832008-01-11 16:17:00 +0000460 and/or commas, for example ``'x y'`` or ``'x, y'``. Alternatively, *fieldnames*
461 can be a sequence of strings such as ``['x', 'y']``.
Georg Brandl9afde1c2007-11-01 20:32:30 +0000462
463 Any valid Python identifier may be used for a fieldname except for names
Christian Heimes0449f632007-12-15 01:27:15 +0000464 starting with an underscore. Valid identifiers consist of letters, digits,
465 and underscores but do not start with a digit or underscore and cannot be
Georg Brandlf6945182008-02-01 11:56:49 +0000466 a :mod:`keyword` such as *class*, *for*, *return*, *global*, *pass*,
Georg Brandl9afde1c2007-11-01 20:32:30 +0000467 or *raise*.
Georg Brandl116aa622007-08-15 14:28:22 +0000468
Christian Heimes25bb7832008-01-11 16:17:00 +0000469 If *verbose* is true, the class definition is printed just before being built.
Georg Brandl116aa622007-08-15 14:28:22 +0000470
Georg Brandl9afde1c2007-11-01 20:32:30 +0000471 Named tuple instances do not have per-instance dictionaries, so they are
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000472 lightweight and require no more memory than regular tuples.
Georg Brandl116aa622007-08-15 14:28:22 +0000473
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000474Example::
Georg Brandl116aa622007-08-15 14:28:22 +0000475
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000476 >>> Point = namedtuple('Point', 'x y', verbose=True)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000477 class Point(tuple):
478 'Point(x, y)'
Christian Heimes0449f632007-12-15 01:27:15 +0000479
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000480 __slots__ = ()
Christian Heimes0449f632007-12-15 01:27:15 +0000481
Christian Heimesfaf2f632008-01-06 16:59:19 +0000482 _fields = ('x', 'y')
483
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000484 def __new__(cls, x, y):
485 return tuple.__new__(cls, (x, y))
Christian Heimes0449f632007-12-15 01:27:15 +0000486
Christian Heimesfaf2f632008-01-06 16:59:19 +0000487 @classmethod
488 def _make(cls, iterable):
489 'Make a new Point object from a sequence or iterable'
490 result = tuple.__new__(cls, iterable)
491 if len(result) != 2:
492 raise TypeError('Expected 2 arguments, got %d' % len(result))
493 return result
Christian Heimes99170a52007-12-19 02:07:34 +0000494
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000495 def __repr__(self):
496 return 'Point(x=%r, y=%r)' % self
Christian Heimes0449f632007-12-15 01:27:15 +0000497
Christian Heimes99170a52007-12-19 02:07:34 +0000498 def _asdict(t):
Christian Heimes0449f632007-12-15 01:27:15 +0000499 'Return a new dict which maps field names to their values'
Christian Heimes99170a52007-12-19 02:07:34 +0000500 return {'x': t[0], 'y': t[1]}
Christian Heimes0449f632007-12-15 01:27:15 +0000501
502 def _replace(self, **kwds):
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000503 'Return a new Point object replacing specified fields with new values'
Christian Heimesfaf2f632008-01-06 16:59:19 +0000504 result = self._make(map(kwds.pop, ('x', 'y'), self))
505 if kwds:
506 raise ValueError('Got unexpected field names: %r' % kwds.keys())
507 return result
Christian Heimes0449f632007-12-15 01:27:15 +0000508
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000509 x = property(itemgetter(0))
510 y = property(itemgetter(1))
Georg Brandl116aa622007-08-15 14:28:22 +0000511
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000512 >>> p = Point(11, y=22) # instantiate with positional or keyword arguments
Christian Heimes99170a52007-12-19 02:07:34 +0000513 >>> p[0] + p[1] # indexable like the plain tuple (11, 22)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000514 33
515 >>> x, y = p # unpack like a regular tuple
516 >>> x, y
517 (11, 22)
518 >>> p.x + p.y # fields also accessable by name
519 33
520 >>> p # readable __repr__ with a name=value style
521 Point(x=11, y=22)
Georg Brandl116aa622007-08-15 14:28:22 +0000522
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000523Named tuples are especially useful for assigning field names to result tuples returned
524by the :mod:`csv` or :mod:`sqlite3` modules::
525
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000526 EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title, department, paygrade')
Georg Brandl9afde1c2007-11-01 20:32:30 +0000527
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000528 import csv
Christian Heimesfaf2f632008-01-06 16:59:19 +0000529 for emp in map(EmployeeRecord._make, csv.reader(open("employees.csv", "rb"))):
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000530 print(emp.name, emp.title)
531
Georg Brandl9afde1c2007-11-01 20:32:30 +0000532 import sqlite3
533 conn = sqlite3.connect('/companydata')
534 cursor = conn.cursor()
535 cursor.execute('SELECT name, age, title, department, paygrade FROM employees')
Christian Heimesfaf2f632008-01-06 16:59:19 +0000536 for emp in map(EmployeeRecord._make, cursor.fetchall()):
Christian Heimes00412232008-01-10 16:02:19 +0000537 print(emp.name, emp.title)
Georg Brandl9afde1c2007-11-01 20:32:30 +0000538
Christian Heimes99170a52007-12-19 02:07:34 +0000539In addition to the methods inherited from tuples, named tuples support
Christian Heimes2380ac72008-01-09 00:17:24 +0000540three additional methods and one attribute. To prevent conflicts with
541field names, the method and attribute names start with an underscore.
Christian Heimes99170a52007-12-19 02:07:34 +0000542
Christian Heimes790c8232008-01-07 21:14:23 +0000543.. method:: somenamedtuple._make(iterable)
Christian Heimes99170a52007-12-19 02:07:34 +0000544
Christian Heimesfaf2f632008-01-06 16:59:19 +0000545 Class method that makes a new instance from an existing sequence or iterable.
Christian Heimes99170a52007-12-19 02:07:34 +0000546
547::
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000548
Christian Heimesfaf2f632008-01-06 16:59:19 +0000549 >>> t = [11, 22]
550 >>> Point._make(t)
551 Point(x=11, y=22)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000552
Christian Heimes790c8232008-01-07 21:14:23 +0000553.. method:: somenamedtuple._asdict()
Georg Brandl9afde1c2007-11-01 20:32:30 +0000554
555 Return a new dict which maps field names to their corresponding values:
556
557::
558
Christian Heimes0449f632007-12-15 01:27:15 +0000559 >>> p._asdict()
Georg Brandl9afde1c2007-11-01 20:32:30 +0000560 {'x': 11, 'y': 22}
561
Christian Heimes790c8232008-01-07 21:14:23 +0000562.. method:: somenamedtuple._replace(kwargs)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000563
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000564 Return a new instance of the named tuple replacing specified fields with new values:
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000565
566::
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000567
568 >>> p = Point(x=11, y=22)
Christian Heimes0449f632007-12-15 01:27:15 +0000569 >>> p._replace(x=33)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000570 Point(x=33, y=22)
571
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000572 >>> for partnum, record in inventory.items():
Christian Heimes454f37b2008-01-10 00:10:02 +0000573 ... inventory[partnum] = record._replace(price=newprices[partnum], timestamp=time.now())
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000574
Christian Heimes790c8232008-01-07 21:14:23 +0000575.. attribute:: somenamedtuple._fields
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000576
Christian Heimes2380ac72008-01-09 00:17:24 +0000577 Tuple of strings listing the field names. Useful for introspection
Georg Brandl9afde1c2007-11-01 20:32:30 +0000578 and for creating new named tuple types from existing named tuples.
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000579
580::
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000581
Christian Heimes0449f632007-12-15 01:27:15 +0000582 >>> p._fields # view the field names
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000583 ('x', 'y')
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000584
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000585 >>> Color = namedtuple('Color', 'red green blue')
Christian Heimes0449f632007-12-15 01:27:15 +0000586 >>> Pixel = namedtuple('Pixel', Point._fields + Color._fields)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000587 >>> Pixel(11, 22, 128, 255, 0)
Christian Heimes454f37b2008-01-10 00:10:02 +0000588 Pixel(x=11, y=22, red=128, green=255, blue=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000589
Christian Heimes0449f632007-12-15 01:27:15 +0000590To retrieve a field whose name is stored in a string, use the :func:`getattr`
Christian Heimes790c8232008-01-07 21:14:23 +0000591function::
Christian Heimes0449f632007-12-15 01:27:15 +0000592
593 >>> getattr(p, 'x')
594 11
595
Christian Heimes25bb7832008-01-11 16:17:00 +0000596To convert a dictionary to a named tuple, use the double-star-operator [#]_::
Christian Heimes99170a52007-12-19 02:07:34 +0000597
598 >>> d = {'x': 11, 'y': 22}
599 >>> Point(**d)
600 Point(x=11, y=22)
601
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000602Since a named tuple is a regular Python class, it is easy to add or change
Christian Heimes043d6f62008-01-07 17:19:16 +0000603functionality with a subclass. Here is how to add a calculated field and
604a fixed-width print format::
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000605
Christian Heimes043d6f62008-01-07 17:19:16 +0000606 >>> class Point(namedtuple('Point', 'x y')):
Christian Heimes25bb7832008-01-11 16:17:00 +0000607 ... __slots__ = ()
Christian Heimes454f37b2008-01-10 00:10:02 +0000608 ... @property
609 ... def hypot(self):
610 ... return (self.x ** 2 + self.y ** 2) ** 0.5
611 ... def __str__(self):
Christian Heimes25bb7832008-01-11 16:17:00 +0000612 ... 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 +0000613
Christian Heimes25bb7832008-01-11 16:17:00 +0000614 >>> for p in Point(3, 4), Point(14, 5/7.):
Christian Heimes00412232008-01-10 16:02:19 +0000615 ... print(p)
Christian Heimes790c8232008-01-07 21:14:23 +0000616
Christian Heimes25bb7832008-01-11 16:17:00 +0000617 Point: x= 3.000 y= 4.000 hypot= 5.000
618 Point: x=14.000 y= 0.714 hypot=14.018
Christian Heimes043d6f62008-01-07 17:19:16 +0000619
Christian Heimesaf98da12008-01-27 15:18:18 +0000620The subclass shown above sets ``__slots__`` to an empty tuple. This keeps
Christian Heimes679db4a2008-01-18 09:56:22 +0000621keep memory requirements low by preventing the creation of instance dictionaries.
622
Christian Heimes2380ac72008-01-09 00:17:24 +0000623
624Subclassing is not useful for adding new, stored fields. Instead, simply
625create a new named tuple type from the :attr:`_fields` attribute::
626
Christian Heimes25bb7832008-01-11 16:17:00 +0000627 >>> Point3D = namedtuple('Point3D', Point._fields + ('z',))
Christian Heimes2380ac72008-01-09 00:17:24 +0000628
629Default values can be implemented by using :meth:`_replace` to
Christian Heimes790c8232008-01-07 21:14:23 +0000630customize a prototype instance::
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000631
632 >>> Account = namedtuple('Account', 'owner balance transaction_count')
Christian Heimes587c2bf2008-01-19 16:21:02 +0000633 >>> default_account = Account('<owner name>', 0.0, 0)
634 >>> johns_account = default_account._replace(owner='John')
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000635
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000636.. rubric:: Footnotes
637
Christian Heimes99170a52007-12-19 02:07:34 +0000638.. [#] For information on the double-star-operator see
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000639 :ref:`tut-unpacking-arguments` and :ref:`calls`.
Raymond Hettingere4c96ad2008-02-06 01:23:58 +0000640
641
642
643:class:`UserDict` objects
Mark Summerfield8f2d0062008-02-06 13:30:44 +0000644-------------------------
Raymond Hettingere4c96ad2008-02-06 01:23:58 +0000645
646The class, :class:`UserDict` acts as a wrapper around dictionary objects.
647The need for this class has been partially supplanted by the ability to
648subclass directly from :class:`dict`; however, this class can be easier
649to work with because the underlying dictionary is accessible as an
650attribute.
651
652.. class:: UserDict([initialdata])
653
654 Class that simulates a dictionary. The instance's contents are kept in a
655 regular dictionary, which is accessible via the :attr:`data` attribute of
656 :class:`UserDict` instances. If *initialdata* is provided, :attr:`data` is
657 initialized with its contents; note that a reference to *initialdata* will not
658 be kept, allowing it be used for other purposes.
659
660In addition to supporting the methods and operations of mappings,
Raymond Hettingerebcee3f2008-02-06 19:54:00 +0000661:class:`UserDict` instances provide the following attribute:
Raymond Hettingere4c96ad2008-02-06 01:23:58 +0000662
663.. attribute:: UserDict.data
664
665 A real dictionary used to store the contents of the :class:`UserDict` class.
Raymond Hettinger53dbe392008-02-12 20:03:09 +0000666
667
668
669:class:`UserList` objects
670-------------------------
671
672This class acts as a wrapper around list objects. It is a useful base class
673for your own list-like classes which can inherit from them and override
674existing methods or add new ones. In this way, one can add new behaviors to
675lists.
676
677The need for this class has been partially supplanted by the ability to
678subclass directly from :class:`list`; however, this class can be easier
679to work with because the underlying list is accessible as an attribute.
680
681.. class:: UserList([list])
682
683 Class that simulates a list. The instance's contents are kept in a regular
684 list, which is accessible via the :attr:`data` attribute of :class:`UserList`
685 instances. The instance's contents are initially set to a copy of *list*,
686 defaulting to the empty list ``[]``. *list* can be any iterable, for
687 example a real Python list or a :class:`UserList` object.
688
689In addition to supporting the methods and operations of mutable sequences,
690:class:`UserList` instances provide the following attribute:
691
692.. attribute:: UserList.data
693
694 A real :class:`list` object used to store the contents of the
695 :class:`UserList` class.
696
697**Subclassing requirements:** Subclasses of :class:`UserList` are expect to
698offer a constructor which can be called with either no arguments or one
699argument. List operations which return a new sequence attempt to create an
700instance of the actual implementation class. To do so, it assumes that the
701constructor can be called with a single parameter, which is a sequence object
702used as a data source.
703
704If a derived class does not wish to comply with this requirement, all of the
705special methods supported by this class will need to be overridden; please
706consult the sources for information about the methods which need to be provided
707in that case.