~ubuntu-branches/ubuntu/lucid/python2.6/lucid

« back to all changes in this revision

Viewing changes to Doc/library/collections.rst

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-03-11 13:30:19 UTC
  • mto: (10.1.13 sid)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: james.westby@ubuntu.com-20100311133019-sblbooa3uqrkoe70
Tags: upstream-2.6.5~rc2
ImportĀ upstreamĀ versionĀ 2.6.5~rc2

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
:class:`Container`                                ``__contains__``
53
53
:class:`Hashable`                                 ``__hash__``
54
54
:class:`Iterable`                                 ``__iter__``
55
 
:class:`Iterator`          :class:`Iterable`      ``__next__``            ``__iter__``
 
55
:class:`Iterator`          :class:`Iterable`      ``next``                ``__iter__``
56
56
:class:`Sized`                                    ``__len__``
57
57
:class:`Callable`                                 ``__call__``
58
58
 
155
155
   * For more about ABCs, see the :mod:`abc` module and :pep:`3119`.
156
156
 
157
157
 
158
 
.. _deque-objects:
159
 
 
160
158
:class:`deque` objects
161
159
----------------------
162
160
 
163
 
 
164
161
.. class:: deque([iterable[, maxlen]])
165
162
 
166
163
   Returns a new deque object initialized left-to-right (using :meth:`append`) with
309
306
   deque(['c', 'b', 'a'])
310
307
 
311
308
 
312
 
.. _deque-recipes:
313
 
 
314
309
:class:`deque` Recipes
315
310
^^^^^^^^^^^^^^^^^^^^^^
316
311
 
339
334
            yield s / float(n)
340
335
 
341
336
The :meth:`rotate` method provides a way to implement :class:`deque` slicing and
342
 
deletion.  For example, a pure python implementation of ``del d[n]`` relies on
 
337
deletion.  For example, a pure Python implementation of ``del d[n]`` relies on
343
338
the :meth:`rotate` method to position elements to be popped::
344
339
 
345
340
   def delete_nth(d, n):
355
350
stack manipulations such as ``dup``, ``drop``, ``swap``, ``over``, ``pick``,
356
351
``rot``, and ``roll``.
357
352
 
358
 
.. _defaultdict-objects:
359
353
 
360
354
:class:`defaultdict` objects
361
355
----------------------------
362
356
 
363
 
 
364
357
.. class:: defaultdict([default_factory[, ...]])
365
358
 
366
359
   Returns a new dictionary-like object.  :class:`defaultdict` is a subclass of the
367
 
   builtin :class:`dict` class.  It overrides one method and adds one writable
 
360
   built-in :class:`dict` class.  It overrides one method and adds one writable
368
361
   instance variable.  The remaining functionality is the same as for the
369
362
   :class:`dict` class and is not documented here.
370
363
 
406
399
      ``None``, if absent.
407
400
 
408
401
 
409
 
.. _defaultdict-examples:
410
 
 
411
402
:class:`defaultdict` Examples
412
403
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
413
404
 
477
468
   [('blue', set([2, 4])), ('red', set([1, 3]))]
478
469
 
479
470
 
480
 
.. _named-tuple-factory:
481
 
 
482
471
:func:`namedtuple` Factory Function for Tuples with Named Fields
483
472
----------------------------------------------------------------
484
473
 
516
505
.. doctest::
517
506
   :options: +NORMALIZE_WHITESPACE
518
507
 
519
 
   >>> Point = namedtuple('Point', 'x y', verbose=True)
 
508
   >>> Point = namedtuple('Point', 'x y')
 
509
   >>> p = Point(11, y=22)     # instantiate with positional or keyword arguments
 
510
   >>> p[0] + p[1]             # indexable like the plain tuple (11, 22)
 
511
   33
 
512
   >>> x, y = p                # unpack like a regular tuple
 
513
   >>> x, y
 
514
   (11, 22)
 
515
   >>> p.x + p.y               # fields also accessible by name
 
516
   33
 
517
   >>> p                       # readable __repr__ with a name=value style
 
518
   Point(x=11, y=22)
 
519
 
 
520
   >>> Point = namedtuple('Point', 'x y', verbose=True) # show the class definition
520
521
   class Point(tuple):
521
522
           'Point(x, y)'
522
523
   <BLANKLINE>
555
556
           x = _property(_itemgetter(0))
556
557
           y = _property(_itemgetter(1))
557
558
 
558
 
   >>> p = Point(11, y=22)     # instantiate with positional or keyword arguments
559
 
   >>> p[0] + p[1]             # indexable like the plain tuple (11, 22)
560
 
   33
561
 
   >>> x, y = p                # unpack like a regular tuple
562
 
   >>> x, y
563
 
   (11, 22)
564
 
   >>> p.x + p.y               # fields also accessible by name
565
 
   33
566
 
   >>> p                       # readable __repr__ with a name=value style
567
 
   Point(x=11, y=22)
568
 
 
569
559
Named tuples are especially useful for assigning field names to result tuples returned
570
560
by the :mod:`csv` or :mod:`sqlite3` modules::
571
561
 
590
580
 
591
581
   Class method that makes a new instance from an existing sequence or iterable.
592
582
 
593
 
.. doctest::
 
583
   .. doctest::
594
584
 
595
585
      >>> t = [11, 22]
596
586
      >>> Point._make(t)
606
596
.. method:: somenamedtuple._replace(kwargs)
607
597
 
608
598
   Return a new instance of the named tuple replacing specified fields with new
609
 
   values:
610
 
 
611
 
::
 
599
   values::
612
600
 
613
601
      >>> p = Point(x=11, y=22)
614
602
      >>> p._replace(x=33)
622
610
   Tuple of strings listing the field names.  Useful for introspection
623
611
   and for creating new named tuple types from existing named tuples.
624
612
 
625
 
.. doctest::
 
613
   .. doctest::
626
614
 
627
615
      >>> p._fields            # view the field names
628
616
      ('x', 'y')