~ubuntu-branches/ubuntu/karmic/python3.0/karmic

« back to all changes in this revision

Viewing changes to Doc/whatsnew/2.6.rst

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-02-16 17:18:23 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20090216171823-1d5cm5qnnjvmnzzm
Tags: 3.0.1-0ubuntu1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
:Release: |release|
11
11
:Date: |today|
12
12
 
13
 
.. $Id: 2.6.rst 67479 2008-12-02 22:34:15Z guido.van.rossum $
 
13
.. $Id: 2.6.rst 68431 2009-01-09 03:12:39Z benjamin.peterson $
14
14
   Rules for maintenance:
15
15
 
16
16
   * Anyone can add text to this document.  Do not spend very much time
590
590
 
591
591
 
592
592
    def factorial(queue, N):
593
 
        "Compute a factorial."
594
 
        # If N is a multiple of 4, this function will take much longer.
595
 
        if (N % 4) == 0:
596
 
            time.sleep(.05 * N/4)
597
 
 
598
 
        # Calculate the result
599
 
        fact = 1L
600
 
        for i in range(1, N+1):
601
 
            fact = fact * i
602
 
 
603
 
        # Put the result on the queue
604
 
        queue.put(fact)
 
593
        "Compute a factorial."
 
594
        # If N is a multiple of 4, this function will take much longer.
 
595
        if (N % 4) == 0:
 
596
            time.sleep(.05 * N/4)
 
597
 
 
598
        # Calculate the result
 
599
        fact = 1L
 
600
        for i in range(1, N+1):
 
601
            fact = fact * i
 
602
 
 
603
        # Put the result on the queue
 
604
        queue.put(fact)
605
605
 
606
606
    if __name__ == '__main__':
607
 
        queue = Queue()
608
 
 
609
 
        N = 5
610
 
 
611
 
        p = Process(target=factorial, args=(queue, N))
612
 
        p.start()
613
 
        p.join()
614
 
 
615
 
        result = queue.get()
616
 
        print 'Factorial', N, '=', result
 
607
        queue = Queue()
 
608
 
 
609
        N = 5
 
610
 
 
611
        p = Process(target=factorial, args=(queue, N))
 
612
        p.start()
 
613
        p.join()
 
614
 
 
615
        result = queue.get()
 
616
        print 'Factorial', N, '=', result
617
617
 
618
618
A :class:`Queue` is used to communicate the input parameter *N* and
619
619
the result.  The :class:`Queue` object is stored in a global variable.
634
634
    from multiprocessing import Pool
635
635
 
636
636
    def factorial(N, dictionary):
637
 
        "Compute a factorial."
638
 
        ...
 
637
        "Compute a factorial."
 
638
        ...
639
639
    p = Pool(5)
640
640
    result = p.map(factorial, range(1, 1000, 10))
641
641
    for v in result:
642
 
        print v
 
642
        print v
643
643
 
644
644
This produces the following output::
645
645
 
738
738
 
739
739
Curly brackets can be escaped by doubling them::
740
740
 
741
 
     >>> format("Empty dict: {{}}")
 
741
     >>> "Empty dict: {{}}".format()
742
742
     "Empty dict: {}"
743
743
 
744
744
Field names can be integers indicating positional arguments, such as
748
748
    >>> import sys
749
749
    >>> print 'Platform: {0.platform}\nPython version: {0.version}'.format(sys)
750
750
    Platform: darwin
751
 
    Python version: 2.6a1+ (trunk:61261M, Mar  5 2008, 20:29:41) 
 
751
    Python version: 2.6a1+ (trunk:61261M, Mar  5 2008, 20:29:41)
752
752
    [GCC 4.0.1 (Apple Computer, Inc. build 5367)]'
753
753
 
754
754
    >>> import mimetypes
962
962
The primary use of :class:`bytes` in 2.6 will be to write tests of
963
963
object type such as ``isinstance(x, bytes)``.  This will help the 2to3
964
964
converter, which can't tell whether 2.x code intends strings to
965
 
contain either characters or 8-bit bytes; you can now 
966
 
use either :class:`bytes` or :class:`str` to represent your intention 
 
965
contain either characters or 8-bit bytes; you can now
 
966
use either :class:`bytes` or :class:`str` to represent your intention
967
967
exactly, and the resulting code will also be correct in Python 3.0.
968
968
 
969
969
There's also a ``__future__`` import that causes all string literals
1838
1838
  "/cgi-bin/add.py?category=1".  (Contributed by Alexandre Fiori and
1839
1839
  Nubis; :issue:`1817`.)
1840
1840
 
1841
 
  The :func:`parse_qs` and :func:`parse_qsl` functions have been 
 
1841
  The :func:`parse_qs` and :func:`parse_qsl` functions have been
1842
1842
  relocated from the :mod:`cgi` module to the :mod:`urlparse` module.
1843
 
  The versions still available in the :mod:`cgi` module will 
 
1843
  The versions still available in the :mod:`cgi` module will
1844
1844
  trigger :exc:`PendingDeprecationWarning` messages in 2.6
1845
1845
  (:issue:`600362`).
1846
1846
 
1889
1889
     ('id', 'name', 'type', 'size')
1890
1890
 
1891
1891
     >>> var = var_type(1, 'frequency', 'int', 4)
1892
 
     >>> print var[0], var.id           # Equivalent
 
1892
     >>> print var[0], var.id    # Equivalent
1893
1893
     1 1
1894
 
     >>> print var[2], var.type          # Equivalent
 
1894
     >>> print var[2], var.type  # Equivalent
1895
1895
     int int
1896
1896
     >>> var._asdict()
1897
1897
     {'size': 4, 'type': 'int', 'id': 1, 'name': 'frequency'}
1935
1935
* A new window method in the :mod:`curses` module,
1936
1936
  :meth:`chgat`, changes the display attributes for a certain number of
1937
1937
  characters on a single line.  (Contributed by Fabian Kreutz.)
1938
 
  
 
1938
 
1939
1939
  ::
1940
1940
 
1941
1941
     # Boldface text starting at y=0,x=21
2050
2050
 
2051
2051
     >>> list(itertools.product([1,2,3], [4,5,6]))
2052
2052
     [(1, 4), (1, 5), (1, 6),
2053
 
          (2, 4), (2, 5), (2, 6),
2054
 
          (3, 4), (3, 5), (3, 6)]
 
2053
      (2, 4), (2, 5), (2, 6),
 
2054
      (3, 4), (3, 5), (3, 6)]
2055
2055
 
2056
2056
  The optional *repeat* keyword argument is used for taking the
2057
2057
  product of an iterable or a set of iterables with themselves,
2432
2432
  :issue:`742598`, :issue:`1193577`.)
2433
2433
 
2434
2434
* The :mod:`sqlite3` module, maintained by Gerhard Haering,
2435
 
  has been updated from version 2.3.2 in Python 2.5 to 
 
2435
  has been updated from version 2.3.2 in Python 2.5 to
2436
2436
  version 2.4.1.
2437
 
 
 
2437
 
2438
2438
* The :mod:`struct` module now supports the C99 :ctype:`_Bool` type,
2439
2439
  using the format character ``'?'``.
2440
2440
  (Contributed by David Remahl.)
2529
2529
  ``with tempfile.NamedTemporaryFile() as tmp: ...``.
2530
2530
  (Contributed by Alexander Belopolsky; :issue:`2021`.)
2531
2531
 
2532
 
* The :mod:`test.test_support` module gained a number 
2533
 
  of context managers useful for writing tests. 
2534
 
  :func:`EnvironmentVarGuard` is a 
 
2532
* The :mod:`test.test_support` module gained a number
 
2533
  of context managers useful for writing tests.
 
2534
  :func:`EnvironmentVarGuard` is a
2535
2535
  context manager that temporarily changes environment variables and
2536
2536
  automatically restores them to their old values.
2537
2537
 
2546
2546
          f = urllib.urlopen('https://sf.net')
2547
2547
          ...
2548
2548
 
2549
 
  Finally, :func:`check_warnings` resets the :mod:`warning` module's 
 
2549
  Finally, :func:`check_warnings` resets the :mod:`warning` module's
2550
2550
  warning filters and returns an object that will record all warning
2551
2551
  messages triggered (:issue:`3781`)::
2552
2552
 
2586
2586
  :meth:`activeCount` method is renamed to :meth:`active_count`.  Both
2587
2587
  the 2.6 and 3.0 versions of the module support the same properties
2588
2588
  and renamed methods, but don't remove the old methods.  No date has been set
2589
 
  for the deprecation of the old APIs in Python 3.x; the old APIs won't 
 
2589
  for the deprecation of the old APIs in Python 3.x; the old APIs won't
2590
2590
  be removed in any 2.x version.
2591
2591
  (Carried out by several people, most notably Benjamin Peterson.)
2592
2592
 
2643
2643
  (Added by Facundo Batista.)
2644
2644
 
2645
2645
* The Unicode database provided by the :mod:`unicodedata` module
2646
 
  has been updated to version 5.1.0.  (Updated by 
 
2646
  has been updated to version 5.1.0.  (Updated by
2647
2647
  Martin von Loewis; :issue:`3811`.)
2648
2648
 
2649
2649
* The :mod:`warnings` module's :func:`formatwarning` and :func:`showwarning`
2654
2654
  A new function, :func:`catch_warnings`, is a context manager
2655
2655
  intended for testing purposes that lets you temporarily modify the
2656
2656
  warning filters and then restore their original values (:issue:`3781`).
2657
 
  
 
2657
 
2658
2658
* The XML-RPC :class:`SimpleXMLRPCServer` and :class:`DocXMLRPCServer`
2659
2659
  classes can now be prevented from immediately opening and binding to
2660
2660
  their socket by passing True as the ``bind_and_activate``
3217
3217
  set ``__hash__ = None`` in their definitions to indicate
3218
3218
  the fact.
3219
3219
 
 
3220
* String exceptions have been removed.  Attempting to use them raises a
 
3221
  :exc:`TypeError`.
 
3222
 
3220
3223
* The :meth:`__init__` method of :class:`collections.deque`
3221
3224
  now clears any existing contents of the deque
3222
3225
  before adding elements from the iterable.  This change makes the
3224
3227
 
3225
3228
* :meth:`object.__init__` previously accepted arbitrary arguments and
3226
3229
  keyword arguments, ignoring them.  In Python 2.6, this is no longer
3227
 
  allowed and will result in a :exc:`TypeError`.  This will affect 
3228
 
  :meth:`__init__` methods that end up calling the corresponding 
 
3230
  allowed and will result in a :exc:`TypeError`.  This will affect
 
3231
  :meth:`__init__` methods that end up calling the corresponding
3229
3232
  method on :class:`object` (perhaps through using :func:`super`).
3230
3233
  See :issue:`1683368` for discussion.
3231
3234
 
3285
3288
 
3286
3289
The author would like to thank the following people for offering
3287
3290
suggestions, corrections and assistance with various drafts of this
3288
 
article: Georg Brandl, Steve Brown, Nick Coghlan, Ralph Corderoy, 
3289
 
Jim Jewett, Kent Johnson, Chris Lambacher,  Martin Michlmayr, 
 
3291
article: Georg Brandl, Steve Brown, Nick Coghlan, Ralph Corderoy,
 
3292
Jim Jewett, Kent Johnson, Chris Lambacher,  Martin Michlmayr,
3290
3293
Antoine Pitrou, Brian Warner.
3291
3294