blob: 209850815ac681eb052011c8d72da8f28be25041 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001****************************
2 What's New in Python 2.6
3****************************
4
Christian Heimes02781dc2008-03-21 01:11:52 +00005.. XXX add trademark info for Apple, Microsoft, SourceForge.
Thomas Wouters1b7f8912007-09-19 03:06:30 +00006
Georg Brandl116aa622007-08-15 14:28:22 +00007:Author: A.M. Kuchling
8:Release: |release|
9:Date: |today|
10
Christian Heimes5b5e81c2007-12-31 16:14:33 +000011.. $Id: whatsnew26.tex 55746 2007-06-02 18:33:53Z neal.norwitz $
12 Rules for maintenance:
13
14 * Anyone can add text to this document. Do not spend very much time
15 on the wording of your changes, because your text will probably
16 get rewritten to some degree.
17
18 * The maintainer will go through Misc/NEWS periodically and add
19 changes; it's therefore more important to add your changes to
20 Misc/NEWS than to this file.
21
22 * This is not a complete list of every single change; completeness
23 is the purpose of Misc/NEWS. Some changes I consider too small
24 or esoteric to include. If such a change is added to the text,
25 I'll just remove it. (This is another reason you shouldn't spend
26 too much time on writing your addition.)
27
28 * If you want to draw your new text to the attention of the
29 maintainer, add 'XXX' to the beginning of the paragraph or
30 section.
31
32 * It's OK to just add a fragmentary note about a change. For
33 example: "XXX Describe the transmogrify() function added to the
34 socket module." The maintainer will research the change and
35 write the necessary text.
36
37 * You can comment out your additions if you like, but it's not
38 necessary (especially when a final release is some months away).
39
40 * Credit the author of a patch or bugfix. Just the name is
41 sufficient; the e-mail address isn't necessary.
42
43 * It's helpful to add the bug/patch number as a comment:
44
Christian Heimes02781dc2008-03-21 01:11:52 +000045 .. Patch 12345
Christian Heimes5b5e81c2007-12-31 16:14:33 +000046 XXX Describe the transmogrify() function added to the socket
47 module.
48 (Contributed by P.Y. Developer.)
49
Christian Heimes02781dc2008-03-21 01:11:52 +000050 This saves the maintainer the effort of going through the SVN logs
Christian Heimes5b5e81c2007-12-31 16:14:33 +000051 when researching a change.
Georg Brandl116aa622007-08-15 14:28:22 +000052
53This article explains the new features in Python 2.6. No release date for
54Python 2.6 has been set; it will probably be released in mid 2008.
55
56This article doesn't attempt to provide a complete specification of the new
57features, but instead provides a convenient overview. For full details, you
58should refer to the documentation for Python 2.6. If you want to understand the
59complete implementation and design rationale, refer to the PEP for a particular
60new feature.
61
Christian Heimes5b5e81c2007-12-31 16:14:33 +000062.. Compare with previous release in 2 - 3 sentences here.
63 add hyperlink when the documentation becomes available online.
Georg Brandl116aa622007-08-15 14:28:22 +000064
Christian Heimes5b5e81c2007-12-31 16:14:33 +000065.. ========================================================================
66.. Large, PEP-level features and changes should be described here.
67.. Should there be a new section here for 3k migration?
68.. Or perhaps a more general section describing module changes/deprecation?
69.. ========================================================================
Georg Brandl116aa622007-08-15 14:28:22 +000070
Thomas Wouters1b7f8912007-09-19 03:06:30 +000071Python 3.0
72================
73
Georg Brandl9afde1c2007-11-01 20:32:30 +000074The development cycle for Python 2.6 also saw the release of the first
75alphas of Python 3.0, and the development of 3.0 has influenced
76a number of features in 2.6.
77
78Python 3.0 is a far-ranging redesign of Python that breaks
79compatibility with the 2.x series. This means that existing Python
80code will need a certain amount of conversion in order to run on
81Python 3.0. However, not all the changes in 3.0 necessarily break
82compatibility. In cases where new features won't cause existing code
83to break, they've been backported to 2.6 and are described in this
84document in the appropriate place. Some of the 3.0-derived features
85are:
86
87* A :meth:`__complex__` method for converting objects to a complex number.
88* Alternate syntax for catching exceptions: ``except TypeError as exc``.
89* The addition of :func:`functools.reduce` as a synonym for the built-in
90 :func:`reduce` function.
Thomas Wouters1b7f8912007-09-19 03:06:30 +000091
92A new command-line switch, :option:`-3`, enables warnings
93about features that will be removed in Python 3.0. You can run code
94with this switch to see how much work will be necessary to port
Christian Heimes90aa7642007-12-19 02:45:37 +000095code to 3.0. The value of this switch is available
96to Python code as the boolean variable ``sys.py3kwarning``,
97and to C extension code as :cdata:`Py_Py3kWarningFlag`.
Thomas Wouters1b7f8912007-09-19 03:06:30 +000098
Christian Heimes02781dc2008-03-21 01:11:52 +000099Python 3.0 adds several new built-in functions and change the
100semantics of some existing built-ins. Entirely new functions such as
101:func:`bin` have simply been added to Python 2.6, but existing
102built-ins haven't been changed; instead, the :mod:`future_builtins`
103module has versions with the new 3.0 semantics. Code written to be
104compatible with 3.0 can do ``from future_builtins import hex, map``
105as necessary.
106
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000107.. seealso::
108
109 The 3xxx series of PEPs, which describes the development process for
110 Python 3.0 and various features that have been accepted, rejected,
111 or are still under consideration.
112
Christian Heimes90aa7642007-12-19 02:45:37 +0000113
114Development Changes
115==================================================
116
117While 2.6 was being developed, the Python development process
118underwent two significant changes: the developer group
119switched from SourceForge's issue tracker to a customized
120Roundup installation, and the documentation was converted from
121LaTeX to reStructured Text.
122
123
124New Issue Tracker: Roundup
125--------------------------------------------------
126
Christian Heimes02781dc2008-03-21 01:11:52 +0000127For a long time, the Python developers have been growing increasingly
128annoyed by SourceForge's bug tracker. SourceForge's hosted solution
129doesn't permit much customization; for example, it wasn't possible to
130customize the life cycle of issues.
Christian Heimes90aa7642007-12-19 02:45:37 +0000131
Christian Heimes02781dc2008-03-21 01:11:52 +0000132The infrastructure committee of the Python Software Foundation
133therefore posted a call for issue trackers, asking volunteers to set
134up different products and import some of the bugs and patches from
135SourceForge. Four different trackers were examined: Atlassian's `Jira
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000136<http://www.atlassian.com/software/jira/>`__,
137`Launchpad <http://www.launchpad.net>`__,
138`Roundup <http://roundup.sourceforge.net/>`__, and
139Trac <http://trac.edgewall.org/>`__.
140The committee eventually settled on Jira
Christian Heimes02781dc2008-03-21 01:11:52 +0000141and Roundup as the two candidates. Jira is a commercial product that
142offers a no-cost hosted instance to free-software projects; Roundup
143is an open-source project that requires volunteers
144to administer it and a server to host it.
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000145
Christian Heimes02781dc2008-03-21 01:11:52 +0000146After posting a call for volunteers, a new Roundup installation was
147set up at http://bugs.python.org. One installation of Roundup can
148host multiple trackers, and this server now also hosts issue trackers
149for Jython and for the Python web site. It will surely find
150other uses in the future.
151
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000152Hosting is kindly provided by `Upfront Systems <http://www.upfrontsystems.co.za/>`__ of Stellenbosch, South Africa. Martin von Loewis put a
153lot of effort into importing existing bugs and patches from
154SourceForge; his scripts for this import operation are at
155http://svn.python.org/view/tracker/importer/.
Christian Heimes02781dc2008-03-21 01:11:52 +0000156
157.. seealso::
158
Martin v. Löwis2a241ca2008-04-05 18:58:09 +0000159 http://bugs.python.org
160 The Python bug tracker.
Christian Heimes02781dc2008-03-21 01:11:52 +0000161
Martin v. Löwis2a241ca2008-04-05 18:58:09 +0000162 http://bugs.jython.org:
163 The Jython bug tracker.
Christian Heimes02781dc2008-03-21 01:11:52 +0000164
Martin v. Löwis2a241ca2008-04-05 18:58:09 +0000165 http://roundup.sourceforge.net/
166 Roundup downloads and documentation.
Christian Heimes02781dc2008-03-21 01:11:52 +0000167
Christian Heimes90aa7642007-12-19 02:45:37 +0000168
Martin v. Löwis2a241ca2008-04-05 18:58:09 +0000169New Documentation Format: ReStructured Text Using Sphinx
170-----------------------------------------------------------
Christian Heimes90aa7642007-12-19 02:45:37 +0000171
Christian Heimes02781dc2008-03-21 01:11:52 +0000172Since the Python project's inception around 1989, the documentation
173had been written using LaTeX. At that time, most documentation was
Christian Heimes90aa7642007-12-19 02:45:37 +0000174printed out for later study, not viewed online. LaTeX was widely used
Christian Heimes02781dc2008-03-21 01:11:52 +0000175because it provided attractive printed output while remaining
176straightforward to write, once the basic rules of the markup have been
177learned.
Christian Heimes90aa7642007-12-19 02:45:37 +0000178
179LaTeX is still used today for writing technical publications destined
180for printing, but the landscape for programming tools has shifted. We
181no longer print out reams of documentation; instead, we browse through
Christian Heimes02781dc2008-03-21 01:11:52 +0000182it online and HTML has become the most important format to support.
Christian Heimes90aa7642007-12-19 02:45:37 +0000183Unfortunately, converting LaTeX to HTML is fairly complicated, and
184Fred L. Drake Jr., the Python documentation editor for many years,
185spent a lot of time wrestling the conversion process into shape.
186Occasionally people would suggest converting the documentation into
187SGML or, later, XML, but performing a good conversion is a major task
188and no one pursued the task to completion.
189
190During the 2.6 development cycle, Georg Brandl put a substantial
191effort into building a new toolchain called Sphinx
192for processing the documentation.
193The input format is reStructured Text,
194a markup commonly used in the Python community that supports
195custom extensions and directives. Sphinx concentrates
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000196on HTML output, producing attractively styled
Martin v. Löwis2a241ca2008-04-05 18:58:09 +0000197and modern HTML, though printed output is still supported through
198conversion to LaTeX. Sphinx is a standalone package that
199can be used in documenting other projects.
Christian Heimes90aa7642007-12-19 02:45:37 +0000200
201.. seealso::
202
Martin v. Löwis2a241ca2008-04-05 18:58:09 +0000203 :ref:`documenting-index`
204 Describes how to write for Python's documentation.
Christian Heimes90aa7642007-12-19 02:45:37 +0000205
Martin v. Löwis2a241ca2008-04-05 18:58:09 +0000206 `Sphinx <http://sphinx.pocoo.org/>`__
207 Documentation and code for the Sphinx toolchain.
208
209 `Docutils <http://docutils.sf.net>`__
210 The underlying reStructured Text parser and toolset.
Christian Heimes90aa7642007-12-19 02:45:37 +0000211
212
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000213PEP 343: The 'with' statement
214=============================
215
216The previous version, Python 2.5, added the ':keyword:`with`'
217statement an optional feature, to be enabled by a ``from __future__
Guido van Rossumb00324f2007-12-04 01:13:14 +0000218import with_statement`` directive. In 2.6 the statement no longer needs to
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000219be specially enabled; this means that :keyword:`with` is now always a
220keyword. The rest of this section is a copy of the corresponding
221section from "What's New in Python 2.5" document; if you read
222it back when Python 2.5 came out, you can skip the rest of this
223section.
224
225The ':keyword:`with`' statement clarifies code that previously would use
226``try...finally`` blocks to ensure that clean-up code is executed. In this
227section, I'll discuss the statement as it will commonly be used. In the next
228section, I'll examine the implementation details and show how to write objects
229for use with this statement.
230
231The ':keyword:`with`' statement is a new control-flow structure whose basic
232structure is::
233
234 with expression [as variable]:
235 with-block
236
237The expression is evaluated, and it should result in an object that supports the
238context management protocol (that is, has :meth:`__enter__` and :meth:`__exit__`
239methods.
240
241The object's :meth:`__enter__` is called before *with-block* is executed and
242therefore can run set-up code. It also may return a value that is bound to the
243name *variable*, if given. (Note carefully that *variable* is *not* assigned
244the result of *expression*.)
245
246After execution of the *with-block* is finished, the object's :meth:`__exit__`
247method is called, even if the block raised an exception, and can therefore run
248clean-up code.
249
250Some standard Python objects now support the context management protocol and can
251be used with the ':keyword:`with`' statement. File objects are one example::
252
253 with open('/etc/passwd', 'r') as f:
254 for line in f:
255 print line
256 ... more processing code ...
257
258After this statement has executed, the file object in *f* will have been
259automatically closed, even if the :keyword:`for` loop raised an exception part-
260way through the block.
261
262.. note::
263
264 In this case, *f* is the same object created by :func:`open`, because
265 :meth:`file.__enter__` returns *self*.
266
267The :mod:`threading` module's locks and condition variables also support the
268':keyword:`with`' statement::
269
270 lock = threading.Lock()
271 with lock:
272 # Critical section of code
273 ...
274
275The lock is acquired before the block is executed and always released once the
276block is complete.
277
278The new :func:`localcontext` function in the :mod:`decimal` module makes it easy
279to save and restore the current decimal context, which encapsulates the desired
280precision and rounding characteristics for computations::
281
282 from decimal import Decimal, Context, localcontext
283
284 # Displays with default precision of 28 digits
285 v = Decimal('578')
286 print v.sqrt()
287
288 with localcontext(Context(prec=16)):
289 # All code in this block uses a precision of 16 digits.
290 # The original context is restored on exiting the block.
291 print v.sqrt()
292
293
294.. _new-26-context-managers:
295
296Writing Context Managers
297------------------------
298
299Under the hood, the ':keyword:`with`' statement is fairly complicated. Most
300people will only use ':keyword:`with`' in company with existing objects and
301don't need to know these details, so you can skip the rest of this section if
302you like. Authors of new objects will need to understand the details of the
303underlying implementation and should keep reading.
304
305A high-level explanation of the context management protocol is:
306
307* The expression is evaluated and should result in an object called a "context
308 manager". The context manager must have :meth:`__enter__` and :meth:`__exit__`
309 methods.
310
311* The context manager's :meth:`__enter__` method is called. The value returned
Christian Heimes2c181612007-12-17 20:04:13 +0000312 is assigned to *VAR*. If no ``as VAR`` clause is present, the value is simply
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000313 discarded.
314
315* The code in *BLOCK* is executed.
316
317* If *BLOCK* raises an exception, the :meth:`__exit__(type, value, traceback)`
318 is called with the exception details, the same values returned by
319 :func:`sys.exc_info`. The method's return value controls whether the exception
320 is re-raised: any false value re-raises the exception, and ``True`` will result
321 in suppressing it. You'll only rarely want to suppress the exception, because
322 if you do the author of the code containing the ':keyword:`with`' statement will
323 never realize anything went wrong.
324
325* If *BLOCK* didn't raise an exception, the :meth:`__exit__` method is still
326 called, but *type*, *value*, and *traceback* are all ``None``.
327
328Let's think through an example. I won't present detailed code but will only
329sketch the methods necessary for a database that supports transactions.
330
331(For people unfamiliar with database terminology: a set of changes to the
332database are grouped into a transaction. Transactions can be either committed,
333meaning that all the changes are written into the database, or rolled back,
334meaning that the changes are all discarded and the database is unchanged. See
335any database textbook for more information.)
336
337Let's assume there's an object representing a database connection. Our goal will
338be to let the user write code like this::
339
340 db_connection = DatabaseConnection()
341 with db_connection as cursor:
342 cursor.execute('insert into ...')
343 cursor.execute('delete from ...')
344 # ... more operations ...
345
346The transaction should be committed if the code in the block runs flawlessly or
347rolled back if there's an exception. Here's the basic interface for
348:class:`DatabaseConnection` that I'll assume::
349
350 class DatabaseConnection:
351 # Database interface
Christian Heimes2c181612007-12-17 20:04:13 +0000352 def cursor(self):
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000353 "Returns a cursor object and starts a new transaction"
Christian Heimes2c181612007-12-17 20:04:13 +0000354 def commit(self):
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000355 "Commits current transaction"
Christian Heimes2c181612007-12-17 20:04:13 +0000356 def rollback(self):
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000357 "Rolls back current transaction"
358
359The :meth:`__enter__` method is pretty easy, having only to start a new
360transaction. For this application the resulting cursor object would be a useful
361result, so the method will return it. The user can then add ``as cursor`` to
362their ':keyword:`with`' statement to bind the cursor to a variable name. ::
363
364 class DatabaseConnection:
365 ...
Christian Heimes2c181612007-12-17 20:04:13 +0000366 def __enter__(self):
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000367 # Code to start a new transaction
368 cursor = self.cursor()
369 return cursor
370
371The :meth:`__exit__` method is the most complicated because it's where most of
372the work has to be done. The method has to check if an exception occurred. If
373there was no exception, the transaction is committed. The transaction is rolled
374back if there was an exception.
375
376In the code below, execution will just fall off the end of the function,
377returning the default value of ``None``. ``None`` is false, so the exception
378will be re-raised automatically. If you wished, you could be more explicit and
379add a :keyword:`return` statement at the marked location. ::
380
381 class DatabaseConnection:
382 ...
Christian Heimes2c181612007-12-17 20:04:13 +0000383 def __exit__(self, type, value, tb):
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000384 if tb is None:
385 # No exception, so commit
386 self.commit()
387 else:
388 # Exception occurred, so rollback.
389 self.rollback()
390 # return False
391
392
393.. _module-contextlib:
394
395The contextlib module
396---------------------
397
398The new :mod:`contextlib` module provides some functions and a decorator that
399are useful for writing objects for use with the ':keyword:`with`' statement.
400
401The decorator is called :func:`contextmanager`, and lets you write a single
402generator function instead of defining a new class. The generator should yield
403exactly one value. The code up to the :keyword:`yield` will be executed as the
404:meth:`__enter__` method, and the value yielded will be the method's return
405value that will get bound to the variable in the ':keyword:`with`' statement's
406:keyword:`as` clause, if any. The code after the :keyword:`yield` will be
407executed in the :meth:`__exit__` method. Any exception raised in the block will
408be raised by the :keyword:`yield` statement.
409
410Our database example from the previous section could be written using this
411decorator as::
412
413 from contextlib import contextmanager
414
415 @contextmanager
Christian Heimes2c181612007-12-17 20:04:13 +0000416 def db_transaction(connection):
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000417 cursor = connection.cursor()
418 try:
419 yield cursor
420 except:
421 connection.rollback()
422 raise
423 else:
424 connection.commit()
425
426 db = DatabaseConnection()
427 with db_transaction(db) as cursor:
428 ...
429
430The :mod:`contextlib` module also has a :func:`nested(mgr1, mgr2, ...)` function
431that combines a number of context managers so you don't need to write nested
432':keyword:`with`' statements. In this example, the single ':keyword:`with`'
433statement both starts a database transaction and acquires a thread lock::
434
435 lock = threading.Lock()
436 with nested (db_transaction(db), lock) as (cursor, locked):
437 ...
438
439Finally, the :func:`closing(object)` function returns *object* so that it can be
440bound to a variable, and calls ``object.close`` at the end of the block. ::
441
442 import urllib, sys
443 from contextlib import closing
444
445 with closing(urllib.urlopen('http://www.yahoo.com')) as f:
446 for line in f:
447 sys.stdout.write(line)
448
449
450.. seealso::
451
452 :pep:`343` - The "with" statement
453 PEP written by Guido van Rossum and Nick Coghlan; implemented by Mike Bland,
454 Guido van Rossum, and Neal Norwitz. The PEP shows the code generated for a
455 ':keyword:`with`' statement, which can be helpful in learning how the statement
456 works.
457
458 The documentation for the :mod:`contextlib` module.
459
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000460.. ======================================================================
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000461
Christian Heimes90aa7642007-12-19 02:45:37 +0000462.. _pep-0366:
463
464PEP 366: Explicit Relative Imports From a Main Module
465============================================================
466
467Python's :option:`-m` switch allows running a module as a script.
468When you ran a module that was located inside a package, relative
469imports didn't work correctly.
470
471The fix in Python 2.6 adds a :attr:`__package__` attribute to modules.
472When present, relative imports will be relative to the value of this
473attribute instead of the :attr:`__name__` attribute. PEP 302-style
474importers can then set :attr:`__package__`. The :mod:`runpy` module
475that implements the :option:`-m` switch now does this, so relative imports
476can now be used in scripts running from inside a package.
477
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000478.. ======================================================================
Christian Heimes90aa7642007-12-19 02:45:37 +0000479
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000480.. ::
481
482 .. _pep-0370:
483
484 PEP 370: XXX
485 =====================================================
486
487 When you run Python, the module search page ``sys.modules`` usually
488 includes a directory whose path ends in ``"site-packages"``. This
489 directory is intended to hold locally-installed packages available to
490 all users on a machine or using a particular site installation.
491
492 Python 2.6 introduces a convention for user-specific site directories.
493
494 .. seealso::
495
496 :pep:`370` - XXX
Martin v. Löwis2a241ca2008-04-05 18:58:09 +0000497 PEP written by XXX; implemented by Christian Heimes.
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000498
499
500.. ======================================================================
501
Christian Heimes78644762008-03-04 23:39:23 +0000502.. _pep-3101:
503
504PEP 3101: Advanced String Formatting
505=====================================================
506
Christian Heimes02781dc2008-03-21 01:11:52 +0000507In Python 3.0, the `%` operator is supplemented by a more powerful
508string formatting method, :meth:`format`. Support for the
509:meth:`format` method has been backported to Python 2.6.
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000510
Christian Heimes02781dc2008-03-21 01:11:52 +0000511In 2.6, both 8-bit and Unicode strings have a `.format()` method that
512treats the string as a template and takes the arguments to be formatted.
513The formatting template uses curly brackets (`{`, `}`) as special characters::
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000514
Christian Heimes02781dc2008-03-21 01:11:52 +0000515 # Substitute positional argument 0 into the string.
516 "User ID: {0}".format("root") -> "User ID: root"
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000517
Christian Heimes02781dc2008-03-21 01:11:52 +0000518 # Use the named keyword arguments
519 uid = 'root'
520
521 'User ID: {uid} Last seen: {last_login}'.format(uid='root',
522 last_login = '5 Mar 2008 07:20') ->
523 'User ID: root Last seen: 5 Mar 2008 07:20'
524
525Curly brackets can be escaped by doubling them::
526
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000527 format("Empty dict: {{}}") -> "Empty dict: {}"
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000528
Christian Heimes02781dc2008-03-21 01:11:52 +0000529Field names can be integers indicating positional arguments, such as
530``{0}``, ``{1}``, etc. or names of keyword arguments. You can also
531supply compound field names that read attributes or access dictionary keys::
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000532
Christian Heimes02781dc2008-03-21 01:11:52 +0000533 import sys
534 'Platform: {0.platform}\nPython version: {0.version}'.format(sys) ->
535 'Platform: darwin\n
536 Python version: 2.6a1+ (trunk:61261M, Mar 5 2008, 20:29:41) \n
537 [GCC 4.0.1 (Apple Computer, Inc. build 5367)]'
538
539 import mimetypes
540 'Content-type: {0[.mp4]}'.format(mimetypes.types_map) ->
541 'Content-type: video/mp4'
542
543Note that when using dictionary-style notation such as ``[.mp4]``, you
544don't need to put any quotation marks around the string; it will look
545up the value using ``.mp4`` as the key. Strings beginning with a
546number will be converted to an integer. You can't write more
547complicated expressions inside a format string.
548
549So far we've shown how to specify which field to substitute into the
550resulting string. The precise formatting used is also controllable by
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000551adding a colon followed by a format specifier. For example::
Christian Heimes02781dc2008-03-21 01:11:52 +0000552
553 # Field 0: left justify, pad to 15 characters
554 # Field 1: right justify, pad to 6 characters
555 fmt = '{0:15} ${1:>6}'
556 fmt.format('Registration', 35) ->
557 'Registration $ 35'
558 fmt.format('Tutorial', 50) ->
559 'Tutorial $ 50'
560 fmt.format('Banquet', 125) ->
561 'Banquet $ 125'
562
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000563Format specifiers can reference other fields through nesting::
Christian Heimes02781dc2008-03-21 01:11:52 +0000564
565 fmt = '{0:{1}}'
Christian Heimes02781dc2008-03-21 01:11:52 +0000566 fmt.format('Invoice #1234', 15) ->
567 'Invoice #1234 '
Christian Heimesf6cd9672008-03-26 13:45:42 +0000568 width = 35
569 fmt.format('Invoice #1234', width) ->
570 'Invoice #1234 '
Christian Heimes02781dc2008-03-21 01:11:52 +0000571
572The alignment of a field within the desired width can be specified:
573
574================ ============================================
575Character Effect
576================ ============================================
577< (default) Left-align
578> Right-align
579^ Center
580= (For numeric types only) Pad after the sign.
581================ ============================================
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000582
Christian Heimesf6cd9672008-03-26 13:45:42 +0000583Format specifiers can also include a presentation type, which
584controls how the value is formatted. For example, floating-point numbers
585can be formatted as a general number or in exponential notation:
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000586
Christian Heimesf6cd9672008-03-26 13:45:42 +0000587 >>> '{0:g}'.format(3.75)
588 '3.75'
589 >>> '{0:e}'.format(3.75)
590 '3.750000e+00'
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000591
Christian Heimesf6cd9672008-03-26 13:45:42 +0000592A variety of presentation types are available. Consult the 2.6
593documentation for a complete list (XXX add link, once it's in the 2.6
594docs), but here's a sample::
595
596 'b' - Binary. Outputs the number in base 2.
597 'c' - Character. Converts the integer to the corresponding
598 Unicode character before printing.
599 'd' - Decimal Integer. Outputs the number in base 10.
600 'o' - Octal format. Outputs the number in base 8.
601 'x' - Hex format. Outputs the number in base 16, using lower-
602 case letters for the digits above 9.
603 'e' - Exponent notation. Prints the number in scientific
604 notation using the letter 'e' to indicate the exponent.
605 'g' - General format. This prints the number as a fixed-point
606 number, unless the number is too large, in which case
607 it switches to 'e' exponent notation.
608 'n' - Number. This is the same as 'g', except that it uses the
609 current locale setting to insert the appropriate
610 number separator characters.
611 '%' - Percentage. Multiplies the number by 100 and displays
612 in fixed ('f') format, followed by a percent sign.
613
614Classes and types can define a __format__ method to control how they're
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000615formatted. It receives a single argument, the format specifier::
616
617 def __format__(self, format_spec):
618 if isinstance(format_spec, unicode):
619 return unicode(str(self))
620 else:
621 return str(self)
622
623There's also a format() built-in that will format a single value. It calls
624the type's :meth:`__format__` method with the provided specifier::
625
626 >>> format(75.6564, '.2f')
627 '75.66'
628
629.. seealso::
630
631 :pep:`3101` - Advanced String Formatting
632 PEP written by Talin.
Christian Heimes78644762008-03-04 23:39:23 +0000633
634.. ======================================================================
635
Christian Heimes02781dc2008-03-21 01:11:52 +0000636.. _pep-3105:
637
638PEP 3105: ``print`` As a Function
639=====================================================
640
641The ``print`` statement becomes the :func:`print` function in Python 3.0.
Martin v. Löwis2a241ca2008-04-05 18:58:09 +0000642Making :func:`print` a function makes it easier to change
643by doing 'def print(...)' or importing a new function from somewhere else.
Christian Heimes02781dc2008-03-21 01:11:52 +0000644
645Python 2.6 has a ``__future__`` import that removes ``print`` as language
646syntax, letting you use the functional form instead. For example::
647
Christian Heimes02781dc2008-03-21 01:11:52 +0000648 from __future__ import print_function
649 print('# of entries', len(dictionary), file=sys.stderr)
650
651The signature of the new function is::
652
653 def print(*args, sep=' ', end='\n', file=None)
654
655The parameters are:
656
657 * **args**: positional arguments whose values will be printed out.
658 * **sep**: the separator, which will be printed between arguments.
659 * **end**: the ending text, which will be printed after all of the
660 arguments have been output.
661 * **file**: the file object to which the output will be sent.
662
663.. seealso::
664
665 :pep:`3105` - Make print a function
666 PEP written by Georg Brandl.
667
668.. ======================================================================
669
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000670.. _pep-3110:
671
672PEP 3110: Exception-Handling Changes
673=====================================================
674
675One error that Python programmers occasionally make
676is the following::
677
678 try:
679 ...
680 except TypeError, ValueError:
681 ...
682
683The author is probably trying to catch both
684:exc:`TypeError` and :exc:`ValueError` exceptions, but this code
685actually does something different: it will catch
686:exc:`TypeError` and bind the resulting exception object
687to the local name ``"ValueError"``. The correct code
688would have specified a tuple::
689
690 try:
691 ...
692 except (TypeError, ValueError):
693 ...
694
695This error is possible because the use of the comma here is ambiguous:
696does it indicate two different nodes in the parse tree, or a single
697node that's a tuple.
698
699Python 3.0 changes the syntax to make this unambiguous by replacing
700the comma with the word "as". To catch an exception and store the
701exception object in the variable ``exc``, you must write::
702
703 try:
704 ...
705 except TypeError as exc:
706 ...
707
708Python 3.0 will only support the use of "as", and therefore interprets
709the first example as catching two different exceptions. Python 2.6
710supports both the comma and "as", so existing code will continue to
711work.
712
713.. seealso::
714
715 :pep:`3110` - Catching Exceptions in Python 3000
716 PEP written and implemented by Collin Winter.
717
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000718.. ======================================================================
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000719
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000720.. _pep-3112:
721
722PEP 3112: Byte Literals
723=====================================================
724
725Python 3.0 adopts Unicode as the language's fundamental string type, and
726denotes 8-bit literals differently, either as ``b'string'``
727or using a :class:`bytes` constructor. For future compatibility,
728Python 2.6 adds :class:`bytes` as a synonym for the :class:`str` type,
729and it also supports the ``b''`` notation.
730
731.. seealso::
732
733 :pep:`3112` - Bytes literals in Python 3000
734 PEP written by Jason Orendorff; backported to 2.6 by Christian Heimes.
735
736.. ======================================================================
737
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000738.. _pep-3116:
739
740PEP 3116: New I/O Library
741=====================================================
742
743XXX write this.
744
745.. seealso::
746
747 :pep:`3116` - New I/O
748 PEP written by Daniel Stutzbach, Mike Verdone, and Guido van Rossum.
749 XXX code written by who?
750
751.. ======================================================================
752
Christian Heimesf6cd9672008-03-26 13:45:42 +0000753.. _pep-3118:
754
755PEP 3118: Revised Buffer Protocol
756=====================================================
757
Martin v. Löwis2a241ca2008-04-05 18:58:09 +0000758The buffer protocol is a C-level API that lets Python types
759exchange pointers into their internal representations. A
760memory-mapped file can be viewed as a buffer of characters, for
761example, and this lets another module such as :mod:`re`
762treat memory-mapped files as a string of characters to be searched.
763
764The primary users of the buffer protocol are numeric-processing
765packages such as NumPy, which can expose the internal representation
766of arrays so that callers can write data directly into an array instead
767of going through a slower API. This PEP updates the buffer protocol in light of experience
768from NumPy development, adding a number of new features
769such as indicating the shape of an array,
770locking memory .
771
772The most important new C API function is
773``PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)``, which
774takes an object and a set of flags, and fills in the
775``Py_buffer`` structure with information
776about the object's memory representation. Objects
777can use this operation to lock memory in place
778while an external caller could be modifying the contents,
779so there's a corresponding
780``PyObject_ReleaseBuffer(PyObject *obj, Py_buffer *view)`` to
781indicate that the external caller is done.
782
783The **flags** argument to :cfunc:`PyObject_GetBuffer` specifies
784constraints upon the memory returned. Some examples are:
785
786 * :const:`PyBUF_WRITABLE` indicates that the memory must be writable.
787
788 * :const:`PyBUF_LOCK` requests a read-only or exclusive lock on the memory.
789
790 * :const:`PyBUF_C_CONTIGUOUS` and :const:`PyBUF_F_CONTIGUOUS`
791 requests a C-contiguous (last dimension varies the fastest) or
792 Fortran-contiguous (first dimension varies the fastest) layout.
793
794.. XXX this feature is not in 2.6 docs yet
Christian Heimesf6cd9672008-03-26 13:45:42 +0000795
796.. seealso::
797
798 :pep:`3118` - Revising the buffer protocol
Martin v. Löwis2a241ca2008-04-05 18:58:09 +0000799 PEP written by Travis Oliphant and Carl Banks; implemented by
800 Travis Oliphant.
801
Christian Heimesf6cd9672008-03-26 13:45:42 +0000802
803.. ======================================================================
804
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000805.. _pep-3119:
806
807PEP 3119: Abstract Base Classes
808=====================================================
809
Martin v. Löwis2a241ca2008-04-05 18:58:09 +0000810Some object-oriented languages such as Java support interfaces: declarations
811that a class has a given set of methods or supports a given access protocol.
812Abstract Base Classes (or ABCs) are an equivalent feature for Python. The ABC
813support consists of an :mod:`abc` module containing a metaclass called
814:class:`ABCMeta`, special handling
815of this metaclass by the :func:`isinstance` and :func:`issubclass` built-ins,
816and a collection of basic ABCs that the Python developers think will be widely
817useful.
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000818
Martin v. Löwis2a241ca2008-04-05 18:58:09 +0000819Let's say you have a particular class and wish to know whether it supports
820dictionary-style access. The phrase "dictionary-style" is vague, however.
821It probably means that accessing items with ``obj[1]`` works.
822Does it imply that setting items with ``obj[2] = value`` works?
823Or that the object will have :meth:`keys`, :meth:`values`, and :meth:`items`
824methods? What about the iterative variants such as :meth:`iterkeys`? :meth:`copy`
825and :meth:`update`? Iterating over the object with :func:`iter`?
Christian Heimes3f419af2008-01-04 03:08:33 +0000826
Martin v. Löwis2a241ca2008-04-05 18:58:09 +0000827Python 2.6 includes a number of different ABCs in the :mod:`collections`
828module. :class:`Iterable` indicates that a class defines :meth:`__iter__`,
829and :class:`Container` means the class supports ``x in y`` expressions
830by defining a :meth:`__contains__` method. The basic dictionary interface of
831getting items, setting items, and
832:meth:`keys`, :meth:`values`, and :meth:`items`, is defined by the
833:class:`MutableMapping` ABC.
Christian Heimes3f419af2008-01-04 03:08:33 +0000834
Martin v. Löwis2a241ca2008-04-05 18:58:09 +0000835You can derive your own classes from a particular ABC
836to indicate they support that ABC's interface::
Christian Heimes3f419af2008-01-04 03:08:33 +0000837
Martin v. Löwis2a241ca2008-04-05 18:58:09 +0000838 import collections
839
840 class Storage(collections.MutableMapping):
841 ...
Christian Heimes3f419af2008-01-04 03:08:33 +0000842
Christian Heimes3f419af2008-01-04 03:08:33 +0000843
Martin v. Löwis2a241ca2008-04-05 18:58:09 +0000844Alternatively, you could write the class without deriving from
845the desired ABC and instead register the class by
846calling the ABC's :meth:`register` method::
Christian Heimes3f419af2008-01-04 03:08:33 +0000847
Martin v. Löwis2a241ca2008-04-05 18:58:09 +0000848 import collections
849
850 class Storage:
851 ...
852
853 collections.MutableMapping.register(Storage)
854
855For classes that you write, deriving from the ABC is probably clearer.
856The :meth:`register` method is useful when you've written a new
857ABC that can describe an existing type or class, or if you want
858to declare that some third-party class implements an ABC.
859For example, if you defined a :class:`PrintableType` ABC,
860it's legal to do:
Christian Heimesfaf2f632008-01-06 16:59:19 +0000861
Martin v. Löwis2a241ca2008-04-05 18:58:09 +0000862 # Register Python's types
863 PrintableType.register(int)
864 PrintableType.register(float)
865 PrintableType.register(str)
866
867Classes should obey the semantics specified by an ABC, but
868Python can't check this; it's up to the class author to
869understand the ABC's requirements and to implement the code accordingly.
870
871To check whether an object supports a particular interface, you can
872now write::
873
874 def func(d):
875 if not isinstance(d, collections.MutableMapping):
876 raise ValueError("Mapping object expected, not %r" % d)
877
878(Don't feel that you must now begin writing lots of checks as in the
879above example. Python has a strong tradition of duck-typing, where
880explicit type-checking isn't done and code simply calls methods on
881an object, trusting that those methods will be there and raising an
882exception if they aren't. Be judicious in checking for ABCs
883and only do it where it helps.)
884
885You can write your own ABCs by using ``abc.ABCMeta`` as the
886metaclass in a class definition::
887
888 from abc import ABCMeta
889
890 class Drawable():
891 __metaclass__ = ABCMeta
892
893 def draw(self, x, y, scale=1.0):
894 pass
895
896 def draw_doubled(self, x, y):
897 self.draw(x, y, scale=2.0)
898
899
900 class Square(Drawable):
901 def draw(self, x, y, scale):
902 ...
903
904
905In the :class:`Drawable` ABC above, the :meth:`draw_doubled` method
906renders the object at twice its size and can be implemented in terms
907of other methods described in :class:`Drawable`. Classes implementing
908this ABC therefore don't need to provide their own implementation
909of :meth:`draw_doubled`, though they can do so. An implementation
910of :meth:`draw` is necessary, though; the ABC can't provide
911a useful generic implementation. You
912can apply the ``@abstractmethod`` decorator to methods such as
913:meth:`draw` that must be implemented; Python will
914then raise an exception for classes that
915don't define the method::
916
917 class Drawable():
918 __metaclass__ = ABCMeta
919
920 @abstractmethod
921 def draw(self, x, y, scale):
922 pass
923
924Note that the exception is only raised when you actually
925try to create an instance of a subclass without the method::
926
927 >>> s=Square()
928 Traceback (most recent call last):
929 File "<stdin>", line 1, in <module>
930 TypeError: Can't instantiate abstract class Square with abstract methods draw
931 >>>
932
933Abstract data attributes can be declared using the ``@abstractproperty`` decorator::
934
Christian Heimesfaf2f632008-01-06 16:59:19 +0000935 @abstractproperty
936 def readonly(self):
937 return self._x
Christian Heimes3f419af2008-01-04 03:08:33 +0000938
Martin v. Löwis2a241ca2008-04-05 18:58:09 +0000939Subclasses must then define a :meth:`readonly` property
Christian Heimes3f419af2008-01-04 03:08:33 +0000940
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000941.. seealso::
942
943 :pep:`3119` - Introducing Abstract Base Classes
944 PEP written by Guido van Rossum and Talin.
Martin v. Löwis2a241ca2008-04-05 18:58:09 +0000945 Implemented by Guido van Rossum.
Christian Heimes90aa7642007-12-19 02:45:37 +0000946 Backported to 2.6 by Benjamin Aranguren, with Alex Martelli.
Georg Brandl116aa622007-08-15 14:28:22 +0000947
Christian Heimes679db4a2008-01-18 09:56:22 +0000948.. ======================================================================
949
Christian Heimes78644762008-03-04 23:39:23 +0000950.. _pep-3127:
951
952PEP 3127: Integer Literal Support and Syntax
953=====================================================
954
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000955XXX write this -- this section is currently just brief notes.
Christian Heimes78644762008-03-04 23:39:23 +0000956
957Python 3.0 changes the syntax for octal integer literals, and
958adds supports for binary integers: 0o instad of 0,
959and 0b for binary. Python 2.6 doesn't support this, but a bin()
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000960builtin was added.
961
962XXX changes to the hex/oct builtins
Christian Heimes78644762008-03-04 23:39:23 +0000963
964
965New bin() built-in returns the binary form of a number.
966
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000967.. seealso::
968
969 :pep:`3127` - Integer Literal Support and Syntax
970 PEP written by Patrick Maupin.
971
Christian Heimes78644762008-03-04 23:39:23 +0000972.. ======================================================================
973
974.. _pep-3129:
975
976PEP 3129: Class Decorators
977=====================================================
978
Christian Heimes02781dc2008-03-21 01:11:52 +0000979Decorators have been extended from functions to classes. It's now legal to
980write::
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000981
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000982 @foo
983 @bar
984 class A:
985 pass
986
Christian Heimes02781dc2008-03-21 01:11:52 +0000987This is equivalent to::
988
989 class A:
990 pass
991
992 A = foo(bar(A))
993
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000994XXX need to find a good motivating example.
995
996.. seealso::
997
998 :pep:`3129` - Class Decorators
999 PEP written by Collin Winter.
Christian Heimes78644762008-03-04 23:39:23 +00001000
1001.. ======================================================================
1002
Christian Heimes679db4a2008-01-18 09:56:22 +00001003.. _pep-3141:
1004
1005PEP 3141: A Type Hierarchy for Numbers
1006=====================================================
1007
1008In Python 3.0, several abstract base classes for numeric types,
1009inspired by Scheme's numeric tower, are being added.
1010This change was backported to 2.6 as the :mod:`numbers` module.
1011
1012The most general ABC is :class:`Number`. It defines no operations at
1013all, and only exists to allow checking if an object is a number by
1014doing ``isinstance(obj, Number)``.
1015
1016Numbers are further divided into :class:`Exact` and :class:`Inexact`.
1017Exact numbers can represent values precisely and operations never
1018round off the results or introduce tiny errors that may break the
Christian Heimesc3f30c42008-02-22 16:37:40 +00001019commutativity and associativity properties; inexact numbers may
Christian Heimes679db4a2008-01-18 09:56:22 +00001020perform such rounding or introduce small errors. Integers, long
1021integers, and rational numbers are exact, while floating-point
1022and complex numbers are inexact.
1023
1024:class:`Complex` is a subclass of :class:`Number`. Complex numbers
1025can undergo the basic operations of addition, subtraction,
1026multiplication, division, and exponentiation, and you can retrieve the
1027real and imaginary parts and obtain a number's conjugate. Python's built-in
1028complex type is an implementation of :class:`Complex`.
1029
1030:class:`Real` further derives from :class:`Complex`, and adds
1031operations that only work on real numbers: :func:`floor`, :func:`trunc`,
1032rounding, taking the remainder mod N, floor division,
1033and comparisons.
1034
1035:class:`Rational` numbers derive from :class:`Real`, have
1036:attr:`numerator` and :attr:`denominator` properties, and can be
Christian Heimes3feef612008-02-11 06:19:17 +00001037converted to floats. Python 2.6 adds a simple rational-number class,
Christian Heimes78644762008-03-04 23:39:23 +00001038:class:`Fraction`, in the :mod:`fractions` module. (It's called
1039:class:`Fraction` instead of :class:`Rational` to avoid
1040a name clash with :class:`numbers.Rational`.)
Christian Heimes679db4a2008-01-18 09:56:22 +00001041
1042:class:`Integral` numbers derive from :class:`Rational`, and
1043can be shifted left and right with ``<<`` and ``>>``,
1044combined using bitwise operations such as ``&`` and ``|``,
1045and can be used as array indexes and slice boundaries.
1046
1047In Python 3.0, the PEP slightly redefines the existing built-ins
Christian Heimes78644762008-03-04 23:39:23 +00001048:func:`round`, :func:`math.floor`, :func:`math.ceil`, and adds a new
1049one, :func:`math.trunc`, that's been backported to Python 2.6.
1050:func:`math.trunc` rounds toward zero, returning the closest
Christian Heimes679db4a2008-01-18 09:56:22 +00001051:class:`Integral` that's between the function's argument and zero.
1052
1053.. seealso::
1054
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001055 :pep:`3141` - A Type Hierarchy for Numbers
1056 PEP written by Jeffrey Yasskin.
1057
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +00001058 `Scheme's numerical tower <http://www.gnu.org/software/guile/manual/html_node/Numerical-Tower.html#Numerical-Tower>`__, from the Guile manual.
Christian Heimes679db4a2008-01-18 09:56:22 +00001059
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +00001060 `Scheme's number datatypes <http://schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-9.html#%_sec_6.2>`__ from the R5RS Scheme specification.
Christian Heimes679db4a2008-01-18 09:56:22 +00001061
1062
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001063The :mod:`fractions` Module
Christian Heimes679db4a2008-01-18 09:56:22 +00001064--------------------------------------------------
1065
1066To fill out the hierarchy of numeric types, a rational-number class
Christian Heimes3feef612008-02-11 06:19:17 +00001067has been added as the :mod:`fractions` module. Rational numbers are
Christian Heimes78644762008-03-04 23:39:23 +00001068represented as a fraction, and can exactly represent
Christian Heimes679db4a2008-01-18 09:56:22 +00001069numbers such as two-thirds that floating-point numbers can only
1070approximate.
1071
Christian Heimes3feef612008-02-11 06:19:17 +00001072The :class:`Fraction` constructor takes two :class:`Integral` values
Christian Heimes679db4a2008-01-18 09:56:22 +00001073that will be the numerator and denominator of the resulting fraction. ::
1074
Christian Heimes3feef612008-02-11 06:19:17 +00001075 >>> from fractions import Fraction
1076 >>> a = Fraction(2, 3)
1077 >>> b = Fraction(2, 5)
Christian Heimes679db4a2008-01-18 09:56:22 +00001078 >>> float(a), float(b)
1079 (0.66666666666666663, 0.40000000000000002)
1080 >>> a+b
Christian Heimes3feef612008-02-11 06:19:17 +00001081 Fraction(16, 15)
Christian Heimes679db4a2008-01-18 09:56:22 +00001082 >>> a/b
Christian Heimes3feef612008-02-11 06:19:17 +00001083 Fraction(5, 3)
Christian Heimes679db4a2008-01-18 09:56:22 +00001084
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001085To help in converting floating-point numbers to rationals,
1086the float type now has a :meth:`as_integer_ratio()` method that returns
1087the numerator and denominator for a fraction that evaluates to the same
1088floating-point value::
1089
1090 >>> (2.5) .as_integer_ratio()
1091 (5, 2)
1092 >>> (3.1415) .as_integer_ratio()
1093 (7074029114692207L, 2251799813685248L)
1094 >>> (1./3) .as_integer_ratio()
1095 (6004799503160661L, 18014398509481984L)
1096
1097Note that values that can only be approximated by floating-point
1098numbers, such as 1./3, are not simplified to the number being
1099approximated; the fraction attempts to match the floating-point value
1100**exactly**.
1101
Christian Heimes3feef612008-02-11 06:19:17 +00001102The :mod:`fractions` module is based upon an implementation by Sjoerd
Christian Heimes679db4a2008-01-18 09:56:22 +00001103Mullender that was in Python's :file:`Demo/classes/` directory for a
1104long time. This implementation was significantly updated by Jeffrey
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001105Yasskin.
Christian Heimes679db4a2008-01-18 09:56:22 +00001106
Georg Brandl116aa622007-08-15 14:28:22 +00001107Other Language Changes
1108======================
1109
1110Here are all of the changes that Python 2.6 makes to the core Python language.
1111
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001112* When calling a function using the ``**`` syntax to provide keyword
1113 arguments, you are no longer required to use a Python dictionary;
1114 any mapping will now work::
1115
1116 >>> def f(**kw):
1117 ... print sorted(kw)
1118 ...
1119 >>> ud=UserDict.UserDict()
1120 >>> ud['a'] = 1
1121 >>> ud['b'] = 'string'
1122 >>> f(**ud)
1123 ['a', 'b']
1124
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001125 .. Patch 1686487
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001126
Georg Brandl9afde1c2007-11-01 20:32:30 +00001127* The built-in types now have improved support for extended slicing syntax,
1128 where various combinations of ``(start, stop, step)`` are supplied.
1129 Previously, the support was partial and certain corner cases wouldn't work.
1130 (Implemented by Thomas Wouters.)
1131
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001132 .. Revision 57619
Georg Brandl9afde1c2007-11-01 20:32:30 +00001133
Christian Heimes679db4a2008-01-18 09:56:22 +00001134* Properties now have three attributes, :attr:`getter`,
Christian Heimes90aa7642007-12-19 02:45:37 +00001135 :attr:`setter` and :attr:`deleter`, that are useful shortcuts for
Christian Heimes679db4a2008-01-18 09:56:22 +00001136 adding or modifying a getter, setter or deleter function to an
1137 existing property. You would use them like this::
Christian Heimes90aa7642007-12-19 02:45:37 +00001138
1139 class C(object):
1140 @property
1141 def x(self):
1142 return self._x
1143
1144 @x.setter
1145 def x(self, value):
1146 self._x = value
1147
1148 @x.deleter
1149 def x(self):
1150 del self._x
1151
Christian Heimes679db4a2008-01-18 09:56:22 +00001152 class D(C):
1153 @C.x.getter
1154 def x(self):
1155 return self._x * 2
1156
1157 @x.setter
1158 def x(self, value):
1159 self._x = value / 2
1160
Christian Heimes90aa7642007-12-19 02:45:37 +00001161
Georg Brandl9afde1c2007-11-01 20:32:30 +00001162* C functions and methods that use
1163 :cfunc:`PyComplex_AsCComplex` will now accept arguments that
1164 have a :meth:`__complex__` method. In particular, the functions in the
1165 :mod:`cmath` module will now accept objects with this method.
1166 This is a backport of a Python 3.0 change.
1167 (Contributed by Mark Dickinson.)
1168
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001169 .. Patch #1675423
Georg Brandl9afde1c2007-11-01 20:32:30 +00001170
Christian Heimes90aa7642007-12-19 02:45:37 +00001171 A numerical nicety: when creating a complex number from two floats
1172 on systems that support signed zeros (-0 and +0), the
Christian Heimes78644762008-03-04 23:39:23 +00001173 :func:`complex` constructor will now preserve the sign
Christian Heimes90aa7642007-12-19 02:45:37 +00001174 of the zero.
1175
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001176 .. Patch 1507
Christian Heimes90aa7642007-12-19 02:45:37 +00001177
Christian Heimesa34706f2008-01-04 03:06:10 +00001178* More floating-point features were also added. The :func:`float` function
1179 will now turn the strings ``+nan`` and ``-nan`` into the corresponding
Christian Heimes3f419af2008-01-04 03:08:33 +00001180 IEEE 754 Not A Number values, and ``+inf`` and ``-inf`` into
Christian Heimesa34706f2008-01-04 03:06:10 +00001181 positive or negative infinity. This works on any platform with
1182 IEEE 754 semantics. (Contributed by Christian Heimes.)
1183
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001184 .. Patch 1635
Christian Heimesa34706f2008-01-04 03:06:10 +00001185
Christian Heimes3f419af2008-01-04 03:08:33 +00001186 Other functions in the :mod:`math` module, :func:`isinf` and
1187 :func:`isnan`, return true if their floating-point argument is
Christian Heimesc3f30c42008-02-22 16:37:40 +00001188 infinite or Not A Number.
1189
Christian Heimes3f419af2008-01-04 03:08:33 +00001190 .. Patch 1640
Christian Heimesc3f30c42008-02-22 16:37:40 +00001191
Christian Heimes3f419af2008-01-04 03:08:33 +00001192 The ``math.copysign(x, y)`` function
1193 copies the sign bit of an IEEE 754 number, returning the absolute
1194 value of *x* combined with the sign bit of *y*. For example,
1195 ``math.copysign(1, -0.0)`` returns -1.0. (Contributed by Christian
1196 Heimes.)
1197
Georg Brandl9afde1c2007-11-01 20:32:30 +00001198* Changes to the :class:`Exception` interface
1199 as dictated by :pep:`352` continue to be made. For 2.6,
1200 the :attr:`message` attribute is being deprecated in favor of the
1201 :attr:`args` attribute.
1202
Christian Heimes90aa7642007-12-19 02:45:37 +00001203* The :exc:`GeneratorExit` exception now subclasses
1204 :exc:`BaseException` instead of :exc:`Exception`. This means
1205 that an exception handler that does ``except Exception:``
1206 will not inadvertently catch :exc:`GeneratorExit`.
1207 (Contributed by Chad Austin.)
1208
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001209 .. Patch #1537
Christian Heimes90aa7642007-12-19 02:45:37 +00001210
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001211* Generator objects now have a :attr:`gi_code` attribute that refers to
1212 the original code object backing the generator.
1213 (Contributed by Collin Winter.)
1214
1215 .. Patch #1473257
1216
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001217* The :func:`compile` built-in function now accepts keyword arguments
Georg Brandl9afde1c2007-11-01 20:32:30 +00001218 as well as positional parameters. (Contributed by Thomas Wouters.)
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001219
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001220 .. Patch 1444529
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001221
Thomas Wouters89d996e2007-09-08 17:39:28 +00001222* The :func:`complex` constructor now accepts strings containing
1223 parenthesized complex numbers, letting ``complex(repr(cmplx))``
1224 will now round-trip values. For example, ``complex('(3+4j)')``
1225 now returns the value (3+4j).
1226
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001227 .. Patch 1491866
Thomas Wouters89d996e2007-09-08 17:39:28 +00001228
1229* The string :meth:`translate` method now accepts ``None`` as the
1230 translation table parameter, which is treated as the identity
1231 transformation. This makes it easier to carry out operations
1232 that only delete characters. (Contributed by Bengt Richter.)
1233
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001234 .. Patch 1193128
Thomas Wouters89d996e2007-09-08 17:39:28 +00001235
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001236* The built-in :func:`dir` function now checks for a :meth:`__dir__`
1237 method on the objects it receives. This method must return a list
1238 of strings containing the names of valid attributes for the object,
1239 and lets the object control the value that :func:`dir` produces.
1240 Objects that have :meth:`__getattr__` or :meth:`__getattribute__`
Christian Heimescbf3b5c2007-12-03 21:02:03 +00001241 methods can use this to advertise pseudo-attributes they will honor.
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001242
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001243 .. Patch 1591665
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001244
Christian Heimes02781dc2008-03-21 01:11:52 +00001245* Instance method objects have new attributes for the object and function
1246 comprising the method; the new synonym for :attr:`im_self` is
1247 :attr:`__self__`, and :attr:`im_func` is also available as :attr:`__func__`.
1248 The old names are still supported in Python 2.6; they're gone in 3.0.
1249
Georg Brandl116aa622007-08-15 14:28:22 +00001250* An obscure change: when you use the the :func:`locals` function inside a
1251 :keyword:`class` statement, the resulting dictionary no longer returns free
1252 variables. (Free variables, in this case, are variables referred to in the
1253 :keyword:`class` statement that aren't attributes of the class.)
1254
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001255.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +00001256
1257
1258Optimizations
1259-------------
1260
Guido van Rossum7736b5b2008-01-15 21:44:53 +00001261* Type objects now have a cache of methods that can reduce
1262 the amount of work required to find the correct method implementation
1263 for a particular class; once cached, the interpreter doesn't need to
1264 traverse base classes to figure out the right method to call.
1265 The cache is cleared if a base class or the class itself is modified,
1266 so the cache should remain correct even in the face of Python's dynamic
1267 nature.
1268 (Original optimization implemented by Armin Rigo, updated for
1269 Python 2.6 by Kevin Jacobs.)
1270
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001271 .. Patch 1700288
Guido van Rossum7736b5b2008-01-15 21:44:53 +00001272
Christian Heimes3f419af2008-01-04 03:08:33 +00001273* All of the functions in the :mod:`struct` module have been rewritten in
1274 C, thanks to work at the Need For Speed sprint.
1275 (Contributed by Raymond Hettinger.)
1276
Georg Brandl116aa622007-08-15 14:28:22 +00001277* Internally, a bit is now set in type objects to indicate some of the standard
1278 built-in types. This speeds up checking if an object is a subclass of one of
1279 these types. (Contributed by Neal Norwitz.)
1280
Christian Heimes78644762008-03-04 23:39:23 +00001281* Unicode strings now uses faster code for detecting
1282 whitespace and line breaks; this speeds up the :meth:`split` method
1283 by about 25% and :meth:`splitlines` by 35%.
Christian Heimes02781dc2008-03-21 01:11:52 +00001284 (Contributed by Antoine Pitrou.) Memory usage is reduced
1285 by using pymalloc for the Unicode string's data.
1286
1287* The ``with`` statement now stores the :meth:`__exit__` method on the stack,
Christian Heimesf6cd9672008-03-26 13:45:42 +00001288 producing a small speedup. (Implemented by Jeffrey Yasskin.)
Christian Heimes78644762008-03-04 23:39:23 +00001289
1290* To reduce memory usage, the garbage collector will now clear internal
1291 free lists when garbage-collecting the highest generation of objects.
1292 This may return memory to the OS sooner.
1293
Georg Brandl116aa622007-08-15 14:28:22 +00001294The net result of the 2.6 optimizations is that Python 2.6 runs the pystone
1295benchmark around XX% faster than Python 2.5.
1296
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001297.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +00001298
1299
1300New, Improved, and Deprecated Modules
1301=====================================
1302
1303As usual, Python's standard library received a number of enhancements and bug
1304fixes. Here's a partial list of the most notable changes, sorted alphabetically
1305by module name. Consult the :file:`Misc/NEWS` file in the source tree for a more
1306complete list of changes, or look through the CVS logs for all the details.
1307
Georg Brandl9afde1c2007-11-01 20:32:30 +00001308* The :mod:`bsddb.dbshelve` module now uses the highest pickling protocol
1309 available, instead of restricting itself to protocol 1.
1310 (Contributed by W. Barnes.)
1311
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001312 .. Patch 1551443
Georg Brandl9afde1c2007-11-01 20:32:30 +00001313
1314* A new data type in the :mod:`collections` module: :class:`namedtuple(typename,
Georg Brandl116aa622007-08-15 14:28:22 +00001315 fieldnames)` is a factory function that creates subclasses of the standard tuple
1316 whose fields are accessible by name as well as index. For example::
1317
Georg Brandl9afde1c2007-11-01 20:32:30 +00001318 >>> var_type = collections.namedtuple('variable',
1319 ... 'id name type size')
1320 # Names are separated by spaces or commas.
1321 # 'id, name, type, size' would also work.
Christian Heimes0449f632007-12-15 01:27:15 +00001322 >>> var_type._fields
Georg Brandl9afde1c2007-11-01 20:32:30 +00001323 ('id', 'name', 'type', 'size')
Georg Brandl116aa622007-08-15 14:28:22 +00001324
Georg Brandl9afde1c2007-11-01 20:32:30 +00001325 >>> var = var_type(1, 'frequency', 'int', 4)
1326 >>> print var[0], var.id # Equivalent
1327 1 1
1328 >>> print var[2], var.type # Equivalent
1329 int int
Christian Heimes0449f632007-12-15 01:27:15 +00001330 >>> var._asdict()
Georg Brandl9afde1c2007-11-01 20:32:30 +00001331 {'size': 4, 'type': 'int', 'id': 1, 'name': 'frequency'}
Christian Heimesa156e092008-02-16 07:38:31 +00001332 >>> v2 = var._replace(name='amplitude')
Georg Brandl9afde1c2007-11-01 20:32:30 +00001333 >>> v2
1334 variable(id=1, name='amplitude', type='int', size=4)
Georg Brandl116aa622007-08-15 14:28:22 +00001335
Guido van Rossum7736b5b2008-01-15 21:44:53 +00001336 Where the new :class:`namedtuple` type proved suitable, the standard
1337 library has been modified to return them. For example,
1338 the :meth:`Decimal.as_tuple` method now returns a named tuple with
1339 :attr:`sign`, :attr:`digits`, and :attr:`exponent` fields.
1340
Georg Brandl116aa622007-08-15 14:28:22 +00001341 (Contributed by Raymond Hettinger.)
1342
Georg Brandl9afde1c2007-11-01 20:32:30 +00001343* Another change to the :mod:`collections` module is that the
Christian Heimes895627f2007-12-08 17:28:33 +00001344 :class:`deque` type now supports an optional *maxlen* parameter;
Georg Brandl9afde1c2007-11-01 20:32:30 +00001345 if supplied, the deque's size will be restricted to no more
Christian Heimes895627f2007-12-08 17:28:33 +00001346 than *maxlen* items. Adding more items to a full deque causes
Georg Brandl9afde1c2007-11-01 20:32:30 +00001347 old items to be discarded.
1348
1349 ::
1350
1351 >>> from collections import deque
1352 >>> dq=deque(maxlen=3)
1353 >>> dq
1354 deque([], maxlen=3)
1355 >>> dq.append(1) ; dq.append(2) ; dq.append(3)
1356 >>> dq
1357 deque([1, 2, 3], maxlen=3)
1358 >>> dq.append(4)
1359 >>> dq
1360 deque([2, 3, 4], maxlen=3)
1361
1362 (Contributed by Raymond Hettinger.)
1363
1364* The :mod:`ctypes` module now supports a :class:`c_bool` datatype
1365 that represents the C99 ``bool`` type. (Contributed by David Remahl.)
1366
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001367 .. Patch 1649190
Georg Brandl9afde1c2007-11-01 20:32:30 +00001368
1369 The :mod:`ctypes` string, buffer and array types also have improved
1370 support for extended slicing syntax,
1371 where various combinations of ``(start, stop, step)`` are supplied.
1372 (Implemented by Thomas Wouters.)
1373
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001374 .. Revision 57769
Georg Brandl9afde1c2007-11-01 20:32:30 +00001375
Georg Brandl116aa622007-08-15 14:28:22 +00001376* A new method in the :mod:`curses` module: for a window, :meth:`chgat` changes
Georg Brandl9afde1c2007-11-01 20:32:30 +00001377 the display characters for a certain number of characters on a single line.
Christian Heimesfdab48e2008-01-20 09:06:41 +00001378 (Contributed by Fabian Kreutz.)
Georg Brandl9afde1c2007-11-01 20:32:30 +00001379 ::
Georg Brandl116aa622007-08-15 14:28:22 +00001380
1381 # Boldface text starting at y=0,x=21
1382 # and affecting the rest of the line.
1383 stdscr.chgat(0,21, curses.A_BOLD)
1384
Christian Heimesfdab48e2008-01-20 09:06:41 +00001385 The :class:`Textbox` class in the :mod:`curses.textpad` module
1386 now supports editing in insert mode as well as overwrite mode.
1387 Insert mode is enabled by supplying a true value for the *insert_mode*
1388 parameter when creating the :class:`Textbox` instance.
Georg Brandl116aa622007-08-15 14:28:22 +00001389
Christian Heimes02781dc2008-03-21 01:11:52 +00001390* The :mod:`datetime` module's :meth:`strftime` methods now support a
1391 ``%f`` format code that expands to the number of microseconds in the
1392 object, zero-padded on
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +00001393 the left to six places. (Contributed by Skip Montanaro.)
Christian Heimes02781dc2008-03-21 01:11:52 +00001394
1395 .. Patch 1158
1396
Georg Brandl9afde1c2007-11-01 20:32:30 +00001397* The :mod:`decimal` module was updated to version 1.66 of
1398 `the General Decimal Specification <http://www2.hursley.ibm.com/decimal/decarith.html>`__. New features
1399 include some methods for some basic mathematical functions such as
1400 :meth:`exp` and :meth:`log10`::
1401
1402 >>> Decimal(1).exp()
1403 Decimal("2.718281828459045235360287471")
1404 >>> Decimal("2.7182818").ln()
1405 Decimal("0.9999999895305022877376682436")
1406 >>> Decimal(1000).log10()
1407 Decimal("3")
1408
Guido van Rossum7736b5b2008-01-15 21:44:53 +00001409 The :meth:`as_tuple` method of :class:`Decimal` objects now returns a
1410 named tuple with :attr:`sign`, :attr:`digits`, and :attr:`exponent` fields.
1411
1412 (Implemented by Facundo Batista and Mark Dickinson. Named tuple
1413 support added by Raymond Hettinger.)
1414
1415* The :mod:`difflib` module's :class:`SequenceMatcher` class
1416 now returns named tuples representing matches.
1417 In addition to behaving like tuples, the returned values
1418 also have :attr:`a`, :attr:`b`, and :attr:`size` attributes.
1419 (Contributed by Raymond Hettinger.)
Georg Brandl9afde1c2007-11-01 20:32:30 +00001420
Thomas Wouters89d996e2007-09-08 17:39:28 +00001421* An optional ``timeout`` parameter was added to the
1422 :class:`ftplib.FTP` class constructor as well as the :meth:`connect`
1423 method, specifying a timeout measured in seconds. (Added by Facundo
Christian Heimesaf98da12008-01-27 15:18:18 +00001424 Batista.) Also, the :class:`FTP` class's
1425 :meth:`storbinary` and :meth:`storlines`
1426 now take an optional *callback* parameter that will be called with
1427 each block of data after the data has been sent.
1428 (Contributed by Phil Schwartz.)
1429
1430 .. Patch 1221598
Thomas Wouters89d996e2007-09-08 17:39:28 +00001431
Georg Brandl9afde1c2007-11-01 20:32:30 +00001432* The :func:`reduce` built-in function is also available in the
1433 :mod:`functools` module. In Python 3.0, the built-in is dropped and it's
1434 only available from :mod:`functools`; currently there are no plans
1435 to drop the built-in in the 2.x series. (Patched by
1436 Christian Heimes.)
1437
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001438 .. Patch 1739906
Georg Brandl9afde1c2007-11-01 20:32:30 +00001439
Georg Brandl116aa622007-08-15 14:28:22 +00001440* The :func:`glob.glob` function can now return Unicode filenames if
1441 a Unicode path was used and Unicode filenames are matched within the directory.
1442
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001443 .. Patch #1001604
Georg Brandl116aa622007-08-15 14:28:22 +00001444
1445* The :mod:`gopherlib` module has been removed.
1446
Christian Heimes02781dc2008-03-21 01:11:52 +00001447* A new function in the :mod:`heapq` module: ``merge(iter1, iter2, ...)``
1448 takes any number of iterables that return data *in sorted
1449 order*, and returns a new iterator that returns the contents of all
1450 the iterators, also in sorted order. For example::
Georg Brandl116aa622007-08-15 14:28:22 +00001451
1452 heapq.merge([1, 3, 5, 9], [2, 8, 16]) ->
1453 [1, 2, 3, 5, 8, 9, 16]
1454
Christian Heimes02781dc2008-03-21 01:11:52 +00001455 Another new function, ``heappushpop(heap, item)``,
1456 pushes *item* onto *heap*, then pops off and returns the smallest item.
1457 This is more efficient than making a call to :func:`heappush` and then
1458 :func:`heappop`.
1459
Georg Brandl116aa622007-08-15 14:28:22 +00001460 (Contributed by Raymond Hettinger.)
1461
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001462* An optional ``timeout`` parameter was added to the
1463 :class:`httplib.HTTPConnection` and :class:`HTTPSConnection`
1464 class constructors, specifying a timeout measured in seconds.
1465 (Added by Facundo Batista.)
1466
Guido van Rossum7736b5b2008-01-15 21:44:53 +00001467* Most of the :mod:`inspect` module's functions, such as
1468 :func:`getmoduleinfo` and :func:`getargs`, now return named tuples.
1469 In addition to behaving like tuples, the elements of the return value
1470 can also be accessed as attributes.
1471 (Contributed by Raymond Hettinger.)
1472
Christian Heimes78644762008-03-04 23:39:23 +00001473 Some new functions in the module include
1474 :func:`isgenerator`, :func:`isgeneratorfunction`,
1475 and :func:`isabstract`.
1476
1477* The :mod:`itertools` module gained several new functions.
1478
1479 ``izip_longest(iter1, iter2, ...[, fillvalue])`` makes tuples from
1480 each of the elements; if some of the iterables are shorter than
1481 others, the missing values are set to *fillvalue*. For example::
Georg Brandl116aa622007-08-15 14:28:22 +00001482
1483 itertools.izip_longest([1,2,3], [1,2,3,4,5]) ->
1484 [(1, 1), (2, 2), (3, 3), (None, 4), (None, 5)]
1485
Christian Heimes78644762008-03-04 23:39:23 +00001486 ``product(iter1, iter2, ..., [repeat=N])`` returns the Cartesian product
1487 of the supplied iterables, a set of tuples containing
1488 every possible combination of the elements returned from each iterable. ::
1489
1490 itertools.product([1,2,3], [4,5,6]) ->
1491 [(1, 4), (1, 5), (1, 6),
1492 (2, 4), (2, 5), (2, 6),
1493 (3, 4), (3, 5), (3, 6)]
1494
1495 The optional *repeat* keyword argument is used for taking the
1496 product of an iterable or a set of iterables with themselves,
1497 repeated *N* times. With a single iterable argument, *N*-tuples
1498 are returned::
1499
1500 itertools.product([1,2], repeat=3)) ->
1501 [(1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2),
1502 (2, 1, 1), (2, 1, 2), (2, 2, 1), (2, 2, 2)]
1503
1504 With two iterables, *2N*-tuples are returned. ::
1505
1506 itertools(product([1,2], [3,4], repeat=2) ->
1507 [(1, 3, 1, 3), (1, 3, 1, 4), (1, 3, 2, 3), (1, 3, 2, 4),
1508 (1, 4, 1, 3), (1, 4, 1, 4), (1, 4, 2, 3), (1, 4, 2, 4),
1509 (2, 3, 1, 3), (2, 3, 1, 4), (2, 3, 2, 3), (2, 3, 2, 4),
1510 (2, 4, 1, 3), (2, 4, 1, 4), (2, 4, 2, 3), (2, 4, 2, 4)]
1511
Christian Heimes02781dc2008-03-21 01:11:52 +00001512 ``combinations(iterable, r)`` returns sub-sequences of length *r* from
Christian Heimes78644762008-03-04 23:39:23 +00001513 the elements of *iterable*. ::
1514
1515 itertools.combinations('123', 2) ->
1516 [('1', '2'), ('1', '3'), ('2', '3')]
1517
1518 itertools.combinations('123', 3) ->
1519 [('1', '2', '3')]
1520
1521 itertools.combinations('1234', 3) ->
1522 [('1', '2', '3'), ('1', '2', '4'), ('1', '3', '4'),
1523 ('2', '3', '4')]
1524
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001525 ``permutations(iter[, r])`` returns all the permutations of length *r* of
1526 the iterable's elements. If *r* is not specified, it will default to the
1527 number of elements produced by the iterable.
1528
1529 itertools.permutations([1,2,3,4], 2) ->
1530 [(1, 2), (1, 3), (1, 4),
1531 (2, 1), (2, 3), (2, 4),
1532 (3, 1), (3, 2), (3, 4),
1533 (4, 1), (4, 2), (4, 3)]
1534
Christian Heimes78644762008-03-04 23:39:23 +00001535 ``itertools.chain(*iterables)` is an existing function in
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001536 :mod:`itertools` that gained a new constructor in Python 2.6.
Christian Heimes78644762008-03-04 23:39:23 +00001537 ``itertools.chain.from_iterable(iterable)`` takes a single
1538 iterable that should return other iterables. :func:`chain` will
1539 then return all the elements of the first iterable, then
1540 all the elements of the second, and so on. ::
1541
1542 chain.from_iterable([[1,2,3], [4,5,6]]) ->
1543 [1, 2, 3, 4, 5, 6]
1544
1545 (All contributed by Raymond Hettinger.)
Georg Brandl116aa622007-08-15 14:28:22 +00001546
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001547* The :mod:`logging` module's :class:`FileHandler` class
1548 and its subclasses :class:`WatchedFileHandler`, :class:`RotatingFileHandler`,
1549 and :class:`TimedRotatingFileHandler` now
1550 have an optional *delay* parameter to its constructor. If *delay*
1551 is true, opening of the log file is deferred until the first
1552 :meth:`emit` call is made. (Contributed by Vinay Sajip.)
1553
Georg Brandl116aa622007-08-15 14:28:22 +00001554* The :mod:`macfs` module has been removed. This in turn required the
1555 :func:`macostools.touched` function to be removed because it depended on the
1556 :mod:`macfs` module.
1557
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001558 .. Patch #1490190
Georg Brandl116aa622007-08-15 14:28:22 +00001559
Georg Brandlfceab5a2008-01-19 20:08:23 +00001560* :class:`mmap` objects now have a :meth:`rfind` method that finds
1561 a substring, beginning at the end of the string and searching
1562 backwards. The :meth:`find` method
Christian Heimes78644762008-03-04 23:39:23 +00001563 also gained an *end* parameter containing the index at which to stop
Georg Brandlfceab5a2008-01-19 20:08:23 +00001564 the forward search.
1565 (Contributed by John Lenton.)
1566
Christian Heimesf6cd9672008-03-26 13:45:42 +00001567* (3.0-warning mode) The :mod:`new` module has been removed from
1568 Python 3.0. Importing it therefore triggers a warning message.
Christian Heimes90aa7642007-12-19 02:45:37 +00001569
Christian Heimes78644762008-03-04 23:39:23 +00001570* The :mod:`operator` module gained a
1571 :func:`methodcaller` function that takes a name and an optional
1572 set of arguments, returning a callable that will call
1573 the named function on any arguments passed to it. For example::
1574
1575 >>> # Equivalent to lambda s: s.replace('old', 'new')
1576 >>> replacer = operator.methodcaller('replace', 'old', 'new')
1577 >>> replacer('old wine in old bottles')
1578 'new wine in new bottles'
1579
1580 (Contributed by Georg Brandl, after a suggestion by Gregory Petrosyan.)
1581
1582 The :func:`attrgetter` function now accepts dotted names and performs
1583 the corresponding attribute lookups::
1584
1585 >>> inst_name = operator.attrgetter('__class__.__name__')
1586 >>> inst_name('')
1587 'str'
1588 >>> inst_name(help)
1589 '_Helper'
1590
1591 (Contributed by Georg Brandl, after a suggestion by Barry Warsaw.)
1592
Christian Heimes90aa7642007-12-19 02:45:37 +00001593* New functions in the :mod:`os` module include
1594 ``fchmod(fd, mode)``, ``fchown(fd, uid, gid)``,
1595 and ``lchmod(path, mode)``, on operating systems that support these
1596 functions. :func:`fchmod` and :func:`fchown` let you change the mode
1597 and ownership of an opened file, and :func:`lchmod` changes the mode
1598 of a symlink.
1599
1600 (Contributed by Georg Brandl and Christian Heimes.)
1601
Georg Brandl9afde1c2007-11-01 20:32:30 +00001602* The :func:`os.walk` function now has a ``followlinks`` parameter. If
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001603 set to True, it will follow symlinks pointing to directories and
1604 visit the directory's contents. For backward compatibility, the
1605 parameter's default value is false. Note that the function can fall
1606 into an infinite recursion if there's a symlink that points to a
1607 parent directory.
1608
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001609 .. Patch 1273829
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001610
Georg Brandl9afde1c2007-11-01 20:32:30 +00001611* The ``os.environ`` object's :meth:`clear` method will now unset the
1612 environment variables using :func:`os.unsetenv` in addition to clearing
1613 the object's keys. (Contributed by Martin Horcicka.)
1614
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001615 .. Patch #1181
Georg Brandl9afde1c2007-11-01 20:32:30 +00001616
Thomas Wouters89d996e2007-09-08 17:39:28 +00001617* In the :mod:`os.path` module, the :func:`splitext` function
1618 has been changed to not split on leading period characters.
1619 This produces better results when operating on Unix's dot-files.
1620 For example, ``os.path.splitext('.ipython')``
1621 now returns ``('.ipython', '')`` instead of ``('', '.ipython')``.
1622
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001623 .. Bug #115886
Thomas Wouters89d996e2007-09-08 17:39:28 +00001624
1625 A new function, :func:`relpath(path, start)` returns a relative path
1626 from the ``start`` path, if it's supplied, or from the current
1627 working directory to the destination ``path``. (Contributed by
1628 Richard Barran.)
1629
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001630 .. Patch 1339796
Thomas Wouters89d996e2007-09-08 17:39:28 +00001631
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001632 On Windows, :func:`os.path.expandvars` will now expand environment variables
1633 in the form "%var%", and "~user" will be expanded into the
Georg Brandl9afde1c2007-11-01 20:32:30 +00001634 user's home directory path. (Contributed by Josiah Carlson.)
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001635
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001636 .. Patch 957650
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001637
Georg Brandl9afde1c2007-11-01 20:32:30 +00001638* The Python debugger provided by the :mod:`pdb` module
1639 gained a new command: "run" restarts the Python program being debugged,
1640 and can optionally take new command-line arguments for the program.
1641 (Contributed by Rocky Bernstein.)
1642
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001643 .. Patch #1393667
Georg Brandl9afde1c2007-11-01 20:32:30 +00001644
Christian Heimes78644762008-03-04 23:39:23 +00001645* The :mod:`pickletools` module now has an :func:`optimize` function
1646 that takes a string containing a pickle and removes some unused
1647 opcodes, returning a shorter pickle that contains the same data structure.
1648 (Contributed by Raymond Hettinger.)
1649
Georg Brandl116aa622007-08-15 14:28:22 +00001650* New functions in the :mod:`posix` module: :func:`chflags` and :func:`lchflags`
1651 are wrappers for the corresponding system calls (where they're available).
1652 Constants for the flag values are defined in the :mod:`stat` module; some
1653 possible values include :const:`UF_IMMUTABLE` to signal the file may not be
1654 changed and :const:`UF_APPEND` to indicate that data can only be appended to the
1655 file. (Contributed by M. Levinson.)
1656
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001657 ``os.closerange(*low*, *high*)`` efficiently closes all file descriptors
1658 from *low* to *high*, ignoring any errors and not including *high* itself.
1659 This function is now used by the :mod:`subprocess` module to make starting
1660 processes faster. (Contributed by Georg Brandl.)
1661
1662 .. Patch #1663329
1663
Christian Heimes2380ac72008-01-09 00:17:24 +00001664* The :mod:`pyexpat` module's :class:`Parser` objects now allow setting
1665 their :attr:`buffer_size` attribute to change the size of the buffer
1666 used to hold character data.
1667 (Contributed by Achim Gaedke.)
1668
1669 .. Patch 1137
1670
Christian Heimesaf98da12008-01-27 15:18:18 +00001671* The :mod:`Queue` module now provides queue classes that retrieve entries
1672 in different orders. The :class:`PriorityQueue` class stores
1673 queued items in a heap and retrieves them in priority order,
1674 and :class:`LifoQueue` retrieves the most recently added entries first,
1675 meaning that it behaves like a stack.
1676 (Contributed by Raymond Hettinger.)
1677
Christian Heimes90aa7642007-12-19 02:45:37 +00001678* The :mod:`random` module's :class:`Random` objects can
1679 now be pickled on a 32-bit system and unpickled on a 64-bit
1680 system, and vice versa. Unfortunately, this change also means
1681 that Python 2.6's :class:`Random` objects can't be unpickled correctly
1682 on earlier versions of Python.
1683 (Contributed by Shawn Ligocki.)
1684
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001685 .. Issue 1727780
Christian Heimes90aa7642007-12-19 02:45:37 +00001686
Christian Heimesf6cd9672008-03-26 13:45:42 +00001687 The new ``triangular(low, high, mode)`` function returns random
1688 numbers following a triangular distribution. The returned values
1689 are between *low* and *high*, not including *high* itself, and
1690 with *mode* as the mode, the most frequently occurring value
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +00001691 in the distribution. (Contributed by Wladmir van der Laan and
1692 Raymond Hettinger.)
Christian Heimesf6cd9672008-03-26 13:45:42 +00001693
1694 .. Patch 1681432
1695
Guido van Rossum7736b5b2008-01-15 21:44:53 +00001696* Long regular expression searches carried out by the :mod:`re`
1697 module will now check for signals being delivered, so especially
1698 long searches can now be interrupted.
1699 (Contributed by Josh Hoyt and Ralf Schmitt.)
1700
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001701 .. Patch 846388
Guido van Rossum7736b5b2008-01-15 21:44:53 +00001702
Georg Brandl116aa622007-08-15 14:28:22 +00001703* The :mod:`rgbimg` module has been removed.
1704
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001705* The :mod:`sched` module's :class:`scheduler` instances now
1706 have a read-only :attr:`queue` attribute that returns the
1707 contents of the scheduler's queue, represented as a list of
1708 named tuples with the fields ``(time, priority, action, argument)``.
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +00001709 (Contributed by Raymond Hettinger.)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001710
1711 .. Patch 1861
1712
Christian Heimesf6cd9672008-03-26 13:45:42 +00001713* The :mod:`select` module now has wrapper functions
1714 for the Linux :cfunc:`epoll` and BSD :cfunc:`kqueue` system calls.
1715 Also, a :meth:`modify` method was added to the existing :class:`poll`
1716 objects; ``pollobj.modify(fd, eventmask)`` takes a file descriptor
1717 or file object and an event mask,
1718
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +00001719 (Contributed by Christian Heimes.)
Christian Heimesf6cd9672008-03-26 13:45:42 +00001720
1721 .. Patch 1657
1722
Georg Brandl9afde1c2007-11-01 20:32:30 +00001723* The :mod:`sets` module has been deprecated; it's better to
1724 use the built-in :class:`set` and :class:`frozenset` types.
1725
Christian Heimes5fb7c2a2007-12-24 08:52:31 +00001726* Integrating signal handling with GUI handling event loops
1727 like those used by Tkinter or GTk+ has long been a problem; most
Christian Heimesc3f30c42008-02-22 16:37:40 +00001728 software ends up polling, waking up every fraction of a second.
Christian Heimes5fb7c2a2007-12-24 08:52:31 +00001729 The :mod:`signal` module can now make this more efficient.
1730 Calling ``signal.set_wakeup_fd(fd)`` sets a file descriptor
1731 to be used; when a signal is received, a byte is written to that
1732 file descriptor. There's also a C-level function,
1733 :cfunc:`PySignal_SetWakeupFd`, for setting the descriptor.
1734
1735 Event loops will use this by opening a pipe to create two descriptors,
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +00001736 one for reading and one for writing. The writable descriptor
Christian Heimes5fb7c2a2007-12-24 08:52:31 +00001737 will be passed to :func:`set_wakeup_fd`, and the readable descriptor
1738 will be added to the list of descriptors monitored by the event loop via
1739 :cfunc:`select` or :cfunc:`poll`.
1740 On receiving a signal, a byte will be written and the main event loop
1741 will be woken up, without the need to poll.
1742
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001743 (Contributed by Adam Olsen.)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +00001744
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001745 .. Patch 1583
Christian Heimes5fb7c2a2007-12-24 08:52:31 +00001746
Christian Heimes78644762008-03-04 23:39:23 +00001747 The :func:`siginterrupt` function is now available from Python code,
1748 and allows changing whether signals can interrupt system calls or not.
1749 (Contributed by Ralf Schmitt.)
1750
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +00001751 The :func:`setitimer` and :func:`getitimer` functions have also been
1752 added on systems that support these system calls. :func:`setitimer`
1753 allows setting interval timers that will cause a signal to be
1754 delivered to the process after a specified time, measured in
1755 wall-clock time, consumed process time, or combined process+system
1756 time. (Contributed by Guilherme Polo.)
1757
1758 .. Patch 2240
1759
1760
Thomas Wouters89d996e2007-09-08 17:39:28 +00001761* The :mod:`smtplib` module now supports SMTP over SSL thanks to the
1762 addition of the :class:`SMTP_SSL` class. This class supports an
1763 interface identical to the existing :class:`SMTP` class. Both
1764 class constructors also have an optional ``timeout`` parameter
1765 that specifies a timeout for the initial connection attempt, measured in
1766 seconds.
1767
1768 An implementation of the LMTP protocol (:rfc:`2033`) was also added to
1769 the module. LMTP is used in place of SMTP when transferring e-mail
1770 between agents that don't manage a mail queue.
1771
1772 (SMTP over SSL contributed by Monty Taylor; timeout parameter
1773 added by Facundo Batista; LMTP implemented by Leif
1774 Hedstrom.)
1775
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001776 .. Patch #957003
Thomas Wouters89d996e2007-09-08 17:39:28 +00001777
Christian Heimes679db4a2008-01-18 09:56:22 +00001778* In the :mod:`smtplib` module, SMTP.starttls() now complies with :rfc:`3207`
1779 and forgets any knowledge obtained from the server not obtained from
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001780 the TLS negotiation itself. (Patch contributed by Bill Fenner.)
Christian Heimes679db4a2008-01-18 09:56:22 +00001781
1782 .. Issue 829951
1783
Guido van Rossum7736b5b2008-01-15 21:44:53 +00001784* The :mod:`socket` module now supports TIPC (http://tipc.sf.net),
1785 a high-performance non-IP-based protocol designed for use in clustered
1786 environments. TIPC addresses are 4- or 5-tuples.
1787 (Contributed by Alberto Bertogli.)
1788
1789 .. Patch #1646
Georg Brandlfceab5a2008-01-19 20:08:23 +00001790
1791* The base classes in the :mod:`SocketServer` module now support
1792 calling a :meth:`handle_timeout` method after a span of inactivity
1793 specified by the server's :attr:`timeout` attribute. (Contributed
1794 by Michael Pomraning.)
1795
1796 .. Patch #742598
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001797
1798* The :mod:`struct` module now supports the C99 :ctype:`_Bool` type,
1799 using the format character ``'?'``.
1800 (Contributed by David Remahl.)
Guido van Rossum7736b5b2008-01-15 21:44:53 +00001801
Christian Heimes90aa7642007-12-19 02:45:37 +00001802* A new variable in the :mod:`sys` module,
Guido van Rossum7736b5b2008-01-15 21:44:53 +00001803 :attr:`float_info`, is an object
Christian Heimes90aa7642007-12-19 02:45:37 +00001804 containing information about the platform's floating-point support
Guido van Rossum7736b5b2008-01-15 21:44:53 +00001805 derived from the :file:`float.h` file. Attributes of this object
1806 include
1807 :attr:`mant_dig` (number of digits in the mantissa), :attr:`epsilon`
Christian Heimes90aa7642007-12-19 02:45:37 +00001808 (smallest difference between 1.0 and the next largest value
1809 representable), and several others. (Contributed by Christian Heimes.)
1810
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001811 .. Patch 1534
Christian Heimes90aa7642007-12-19 02:45:37 +00001812
Guido van Rossum7736b5b2008-01-15 21:44:53 +00001813 Another new variable, :attr:`dont_write_bytecode`, controls whether Python
1814 writes any :file:`.pyc` or :file:`.pyo` files on importing a module.
1815 If this variable is true, the compiled files are not written. The
1816 variable is initially set on start-up by supplying the :option:`-B`
1817 switch to the Python interpreter, or by setting the
1818 :envvar:`PYTHONDONTWRITEBYTECODE` environment variable before
1819 running the interpreter. Python code can subsequently
1820 change the value of this variable to control whether bytecode files
1821 are written or not.
1822 (Contributed by Neal Norwitz and Georg Brandl.)
1823
1824 Information about the command-line arguments supplied to the Python
1825 interpreter are available as attributes of a ``sys.flags`` named
1826 tuple. For example, the :attr:`verbose` attribute is true if Python
1827 was executed in verbose mode, :attr:`debug` is true in debugging mode, etc.
1828 These attributes are all read-only.
1829 (Contributed by Christian Heimes.)
1830
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001831 It's now possible to determine the current profiler and tracer functions
1832 by calling :func:`sys.getprofile` and :func:`sys.gettrace`.
1833 (Contributed by Georg Brandl.)
1834
1835 .. Patch #1648
1836
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001837* The :mod:`tarfile` module now supports POSIX.1-2001 (pax) and
1838 POSIX.1-1988 (ustar) format tarfiles, in addition to the GNU tar
1839 format that was already supported. The default format
1840 is GNU tar; specify the ``format`` parameter to open a file
1841 using a different format::
1842
1843 tar = tarfile.open("output.tar", "w", format=tarfile.PAX_FORMAT)
1844
1845 The new ``errors`` parameter lets you specify an error handling
1846 scheme for character conversions: the three standard ways Python can
1847 handle errors ``'strict'``, ``'ignore'``, ``'replace'`` , or the
1848 special value ``'utf-8'``, which replaces bad characters with their
1849 UTF-8 representation. Character conversions occur because the PAX
1850 format supports Unicode filenames, defaulting to UTF-8 encoding.
1851
1852 The :meth:`TarFile.add` method now accepts a ``exclude`` argument that's
1853 a function that can be used to exclude certain filenames from
1854 an archive.
1855 The function must take a filename and return true if the file
1856 should be excluded or false if it should be archived.
1857 The function is applied to both the name initially passed to :meth:`add`
1858 and to the names of files in recursively-added directories.
1859
1860 (All changes contributed by Lars Gustäbel).
1861
Thomas Wouters89d996e2007-09-08 17:39:28 +00001862* An optional ``timeout`` parameter was added to the
1863 :class:`telnetlib.Telnet` class constructor, specifying a timeout
1864 measured in seconds. (Added by Facundo Batista.)
Georg Brandl116aa622007-08-15 14:28:22 +00001865
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001866* The :class:`tempfile.NamedTemporaryFile` class usually deletes
1867 the temporary file it created when the file is closed. This
1868 behaviour can now be changed by passing ``delete=False`` to the
1869 constructor. (Contributed by Damien Miller.)
1870
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001871 .. Patch #1537850
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001872
Christian Heimes78644762008-03-04 23:39:23 +00001873 A new class, :class:`SpooledTemporaryFile`, behaves like
1874 a temporary file but stores its data in memory until a maximum size is
1875 exceeded. On reaching that limit, the contents will be written to
1876 an on-disk temporary file. (Contributed by Dustin J. Mitchell.)
1877
1878 The :class:`NamedTemporaryFile` and :class:`SpooledTemporaryFile` classes
1879 both work as context managers, so you can write
1880 ``with tempfile.NamedTemporaryFile() as tmp: ...``.
1881 (Contributed by Alexander Belopolsky.)
1882
1883 .. Issue #2021
1884
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001885* The :mod:`test.test_support` module now contains a
1886 :func:`EnvironmentVarGuard`
Georg Brandl116aa622007-08-15 14:28:22 +00001887 context manager that supports temporarily changing environment variables and
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001888 automatically restores them to their old values.
1889
1890 Another context manager, :class:`TransientResource`, can surround calls
1891 to resources that may or may not be available; it will catch and
1892 ignore a specified list of exceptions. For example,
1893 a network test may ignore certain failures when connecting to an
1894 external web site::
1895
1896 with test_support.TransientResource(IOError, errno=errno.ETIMEDOUT):
1897 f = urllib.urlopen('https://sf.net')
1898 ...
1899
1900 (Contributed by Brett Cannon.)
1901
1902* The :mod:`textwrap` module can now preserve existing whitespace
1903 at the beginnings and ends of the newly-created lines
1904 by specifying ``drop_whitespace=False``
1905 as an argument::
1906
1907 >>> S = """This sentence has a bunch of extra whitespace."""
1908 >>> print textwrap.fill(S, width=15)
1909 This sentence
1910 has a bunch
1911 of extra
1912 whitespace.
1913 >>> print textwrap.fill(S, drop_whitespace=False, width=15)
1914 This sentence
1915 has a bunch
1916 of extra
1917 whitespace.
1918 >>>
1919
Christian Heimes78644762008-03-04 23:39:23 +00001920 (Contributed by Dwayne Bailey.)
1921
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001922 .. Patch #1581073
Georg Brandl116aa622007-08-15 14:28:22 +00001923
Thomas Wouters89d996e2007-09-08 17:39:28 +00001924* The :mod:`timeit` module now accepts callables as well as strings
1925 for the statement being timed and for the setup code.
1926 Two convenience functions were added for creating
1927 :class:`Timer` instances:
1928 ``repeat(stmt, setup, time, repeat, number)`` and
1929 ``timeit(stmt, setup, time, number)`` create an instance and call
1930 the corresponding method. (Contributed by Erik Demaine.)
1931
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001932 .. Patch #1533909
Thomas Wouters89d996e2007-09-08 17:39:28 +00001933
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001934* An optional ``timeout`` parameter was added to the
1935 :func:`urllib.urlopen` function and the
1936 :class:`urllib.ftpwrapper` class constructor, as well as the
1937 :func:`urllib2.urlopen` function. The parameter specifies a timeout
1938 measured in seconds. For example::
1939
1940 >>> u = urllib2.urlopen("http://slow.example.com", timeout=3)
1941 Traceback (most recent call last):
1942 ...
1943 urllib2.URLError: <urlopen error timed out>
1944 >>>
1945
1946 (Added by Facundo Batista.)
1947
Georg Brandl9afde1c2007-11-01 20:32:30 +00001948* The XML-RPC classes :class:`SimpleXMLRPCServer` and :class:`DocXMLRPCServer`
1949 classes can now be prevented from immediately opening and binding to
1950 their socket by passing True as the ``bind_and_activate``
1951 constructor parameter. This can be used to modify the instance's
1952 :attr:`allow_reuse_address` attribute before calling the
1953 :meth:`server_bind` and :meth:`server_activate` methods to
1954 open the socket and begin listening for connections.
1955 (Contributed by Peter Parente.)
1956
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001957 .. Patch 1599845
Georg Brandl9afde1c2007-11-01 20:32:30 +00001958
1959 :class:`SimpleXMLRPCServer` also has a :attr:`_send_traceback_header`
1960 attribute; if true, the exception and formatted traceback are returned
1961 as HTTP headers "X-Exception" and "X-Traceback". This feature is
1962 for debugging purposes only and should not be used on production servers
1963 because the tracebacks could possibly reveal passwords or other sensitive
1964 information. (Contributed by Alan McIntyre as part of his
1965 project for Google's Summer of Code 2007.)
1966
Guido van Rossum7736b5b2008-01-15 21:44:53 +00001967* The :mod:`zipfile` module's :class:`ZipFile` class now has
1968 :meth:`extract` and :meth:`extractall` methods that will unpack
1969 a single file or all the files in the archive to the current directory, or
1970 to a specified directory::
1971
1972 z = zipfile.ZipFile('python-251.zip')
1973
1974 # Unpack a single file, writing it relative to the /tmp directory.
1975 z.extract('Python/sysmodule.c', '/tmp')
1976
1977 # Unpack all the files in the archive.
1978 z.extractall()
1979
1980 (Contributed by Alan McIntyre.)
Christian Heimesc3f30c42008-02-22 16:37:40 +00001981
1982 .. Patch 467924
Guido van Rossum7736b5b2008-01-15 21:44:53 +00001983
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001984.. ======================================================================
1985.. whole new modules get described in subsections here
Georg Brandl9afde1c2007-11-01 20:32:30 +00001986
1987Improved SSL Support
1988--------------------------------------------------
1989
1990Bill Janssen made extensive improvements to Python 2.6's support for
1991SSL.
1992
1993XXX use ssl.sslsocket - subclass of socket.socket.
1994
1995XXX Can specify if certificate is required, and obtain certificate info
1996by calling getpeercert method.
1997
1998XXX sslwrap() behaves like socket.ssl
1999
2000XXX Certain features require the OpenSSL package to be installed, notably
2001 the 'openssl' binary.
2002
2003.. seealso::
2004
2005 SSL module documentation.
Georg Brandl116aa622007-08-15 14:28:22 +00002006
Christian Heimesaf98da12008-01-27 15:18:18 +00002007
2008.. ======================================================================
2009
2010plistlib: A Property-List Parser
2011--------------------------------------------------
2012
2013A commonly-used format on MacOS X is the ``.plist`` format,
2014which stores basic data types (numbers, strings, lists,
2015and dictionaries) and serializes them into an XML-based format.
2016(It's a lot like the XML-RPC serialization of data types.)
2017
2018Despite being primarily used on MacOS X, the format
2019has nothing Mac-specific about it and the Python implementation works
2020on any platform that Python supports, so the :mod:`plistlib` module
2021has been promoted to the standard library.
2022
2023Using the module is simple::
2024
2025 import sys
2026 import plistlib
2027 import datetime
2028
2029 # Create data structure
2030 data_struct = dict(lastAccessed=datetime.datetime.now(),
2031 version=1,
2032 categories=('Personal', 'Shared', 'Private'))
2033
2034 # Create string containing XML.
2035 plist_str = plistlib.writePlistToString(data_struct)
2036 new_struct = plistlib.readPlistFromString(plist_str)
2037 print data_struct
2038 print new_struct
2039
2040 # Write data structure to a file and read it back.
2041 plistlib.writePlist(data_struct, '/tmp/customizations.plist')
2042 new_struct = plistlib.readPlist('/tmp/customizations.plist')
2043
2044 # read/writePlist accepts file-like objects as well as paths.
2045 plistlib.writePlist(data_struct, sys.stdout)
2046
2047
Christian Heimes5b5e81c2007-12-31 16:14:33 +00002048.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +00002049
2050
2051Build and C API Changes
2052=======================
2053
2054Changes to Python's build process and to the C API include:
2055
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00002056* Python 2.6 can be built with Microsoft Visual Studio 2008.
2057 See the :file:`PCbuild9` directory for the build files.
2058 (Implemented by Christian Heimes.)
2059
Christian Heimes02781dc2008-03-21 01:11:52 +00002060* Python now can only be compiled with C89 compilers (after 19
2061 years!). This means that the Python source tree can now drop its
2062 own implementations of :cfunc:`memmove` and :cfunc:`strerror`, which
2063 are in the C89 standard library.
2064
Georg Brandl9afde1c2007-11-01 20:32:30 +00002065* The BerkeleyDB module now has a C API object, available as
2066 ``bsddb.db.api``. This object can be used by other C extensions
2067 that wish to use the :mod:`bsddb` module for their own purposes.
2068 (Contributed by Duncan Grisby.)
2069
Christian Heimes5b5e81c2007-12-31 16:14:33 +00002070 .. Patch 1551895
Georg Brandl9afde1c2007-11-01 20:32:30 +00002071
Christian Heimes90aa7642007-12-19 02:45:37 +00002072* Several functions return information about the platform's
2073 floating-point support. :cfunc:`PyFloat_GetMax` returns
2074 the maximum representable floating point value,
2075 and :cfunc:`PyFloat_GetMin` returns the minimum
2076 positive value. :cfunc:`PyFloat_GetInfo` returns a dictionary
2077 containing more information from the :file:`float.h` file, such as
2078 ``"mant_dig"`` (number of digits in the mantissa), ``"epsilon"``
2079 (smallest difference between 1.0 and the next largest value
2080 representable), and several others.
Christian Heimesa34706f2008-01-04 03:06:10 +00002081 (Contributed by Christian Heimes.)
Christian Heimes90aa7642007-12-19 02:45:37 +00002082
Christian Heimes5b5e81c2007-12-31 16:14:33 +00002083 .. Issue 1534
Georg Brandl116aa622007-08-15 14:28:22 +00002084
Christian Heimesa34706f2008-01-04 03:06:10 +00002085* Python's C API now includes two functions for case-insensitive string
Christian Heimesc3f30c42008-02-22 16:37:40 +00002086 comparisons, ``PyOS_stricmp(char*, char*)``
Christian Heimesa34706f2008-01-04 03:06:10 +00002087 and ``PyOS_strnicmp(char*, char*, Py_ssize_t)``.
2088 (Contributed by Christian Heimes.)
2089
2090 .. Issue 1635
2091
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002092* Some macros were renamed in both 3.0 and 2.6 to make it clearer that
2093 they are macros,
Christian Heimes3f419af2008-01-04 03:08:33 +00002094 not functions. :cmacro:`Py_Size()` became :cmacro:`Py_SIZE()`,
Christian Heimesa34706f2008-01-04 03:06:10 +00002095 :cmacro:`Py_Type()` became :cmacro:`Py_TYPE()`, and
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002096 :cmacro:`Py_Refcnt()` became :cmacro:`Py_REFCNT()`.
2097 The mixed-case macros are still available
2098 in Python 2.6 for backward compatibility.
Christian Heimesa34706f2008-01-04 03:06:10 +00002099
Christian Heimes3f419af2008-01-04 03:08:33 +00002100 .. Issue 1629
Christian Heimesa34706f2008-01-04 03:06:10 +00002101
Christian Heimesaf98da12008-01-27 15:18:18 +00002102* Distutils now places C extensions it builds in a
2103 different directory when running on a debug version of Python.
2104 (Contributed by Collin Winter.)
2105
2106 .. Patch 1530959
2107
Christian Heimes78644762008-03-04 23:39:23 +00002108* Several basic data types, such as integers and strings, maintain
2109 internal free lists of objects that can be re-used. The data
2110 structures for these free lists now follow a naming convention: the
2111 variable is always named ``free_list``, the counter is always named
2112 ``numfree``, and a macro :cmacro:`Py<typename>_MAXFREELIST` is
2113 always defined.
Christian Heimesaf98da12008-01-27 15:18:18 +00002114
Christian Heimes5b5e81c2007-12-31 16:14:33 +00002115.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +00002116
2117
Christian Heimesa34706f2008-01-04 03:06:10 +00002118Port-Specific Changes: Windows
2119-----------------------------------
2120
2121* The :mod:`msvcrt` module now supports
2122 both the normal and wide char variants of the console I/O
2123 API. The :func:`getwch` function reads a keypress and returns a Unicode
2124 value, as does the :func:`getwche` function. The :func:`putwch` function
2125 takes a Unicode character and writes it to the console.
Christian Heimes679db4a2008-01-18 09:56:22 +00002126 (Contributed by Christian Heimes.)
2127
2128* :func:`os.path.expandvars` will now expand environment variables
2129 in the form "%var%", and "~user" will be expanded into the
2130 user's home directory path. (Contributed by Josiah Carlson.)
2131
2132* The :mod:`socket` module's socket objects now have an
2133 :meth:`ioctl` method that provides a limited interface to the
2134 :cfunc:`WSAIoctl` system interface.
Georg Brandl116aa622007-08-15 14:28:22 +00002135
Guido van Rossum7736b5b2008-01-15 21:44:53 +00002136* The :mod:`_winreg` module now has a function,
2137 :func:`ExpandEnvironmentStrings`,
2138 that expands environment variable references such as ``%NAME%``
2139 in an input string. The handle objects provided by this
2140 module now support the context protocol, so they can be used
Christian Heimes679db4a2008-01-18 09:56:22 +00002141 in :keyword:`with` statements. (Contributed by Christian Heimes.)
2142
2143* The new default compiler on Windows is Visual Studio 2008 (VS 9.0). The
2144 build directories for Visual Studio 2003 (VS7.1) and 2005 (VS8.0)
2145 were moved into the PC/ directory. The new PCbuild directory supports
2146 cross compilation for X64, debug builds and Profile Guided Optimization
2147 (PGO). PGO builds are roughly 10% faster than normal builds.
2148 (Contributed by Christian Heimes with help from Amaury Forgeot d'Arc and
2149 Martin von Loewis.)
Georg Brandl116aa622007-08-15 14:28:22 +00002150
Christian Heimes5b5e81c2007-12-31 16:14:33 +00002151.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +00002152
2153
2154.. _section-other:
2155
2156Other Changes and Fixes
2157=======================
2158
Guido van Rossum7736b5b2008-01-15 21:44:53 +00002159As usual, there were a bunch of other improvements and bugfixes
2160scattered throughout the source tree. A search through the change
2161logs finds there were XXX patches applied and YYY bugs fixed between
2162Python 2.5 and 2.6. Both figures are likely to be underestimates.
Georg Brandl116aa622007-08-15 14:28:22 +00002163
2164Some of the more notable changes are:
2165
Guido van Rossum7736b5b2008-01-15 21:44:53 +00002166* It's now possible to prevent Python from writing any :file:`.pyc`
2167 or :file:`.pyo` files by either supplying the :option:`-B` switch
2168 or setting the :envvar:`PYTHONDONTWRITEBYTECODE` environment variable
2169 to any non-empty string when running the Python interpreter. These
2170 are also used to set the :data:`sys.dont_write_bytecode` attribute;
2171 Python code can change this variable to control whether bytecode
2172 files are subsequently written.
2173 (Contributed by Neal Norwitz and Georg Brandl.)
Georg Brandl116aa622007-08-15 14:28:22 +00002174
Christian Heimes5b5e81c2007-12-31 16:14:33 +00002175.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +00002176
2177
2178Porting to Python 2.6
2179=====================
2180
Christian Heimesf6cd9672008-03-26 13:45:42 +00002181This section lists previously described changes and other bugfixes
2182that may require changes to your code:
Georg Brandl116aa622007-08-15 14:28:22 +00002183
Christian Heimesfaf2f632008-01-06 16:59:19 +00002184* The :meth:`__init__` method of :class:`collections.deque`
Christian Heimesa34706f2008-01-04 03:06:10 +00002185 now clears any existing contents of the deque
2186 before adding elements from the iterable. This change makes the
2187 behavior match that of ``list.__init__()``.
2188
Guido van Rossum7736b5b2008-01-15 21:44:53 +00002189* The :class:`Decimal` constructor now accepts leading and trailing
2190 whitespace when passed a string. Previously it would raise an
2191 :exc:`InvalidOperation` exception. On the other hand, the
2192 :meth:`create_decimal` method of :class:`Context` objects now
2193 explicitly disallows extra whitespace, raising a
2194 :exc:`ConversionSyntax` exception.
2195
2196* Due to an implementation accident, if you passed a file path to
2197 the built-in :func:`__import__` function, it would actually import
2198 the specified file. This was never intended to work, however, and
2199 the implementation now explicitly checks for this case and raises
2200 an :exc:`ImportError`.
2201
Georg Brandl9afde1c2007-11-01 20:32:30 +00002202* The :mod:`socket` module exception :exc:`socket.error` now inherits
2203 from :exc:`IOError`. Previously it wasn't a subclass of
2204 :exc:`StandardError` but now it is, through :exc:`IOError`.
2205 (Implemented by Gregory P. Smith.)
2206
Christian Heimes5b5e81c2007-12-31 16:14:33 +00002207 .. Issue 1706815
Georg Brandl116aa622007-08-15 14:28:22 +00002208
Christian Heimes05e8be12008-02-23 18:30:17 +00002209* The :mod:`xmlrpclib` module no longer automatically converts
2210 :class:`datetime.date` and :class:`datetime.time` to the
2211 :class:`xmlrpclib.DateTime` type; the conversion semantics were
2212 not necessarily correct for all applications. Code using
2213 :mod:`xmlrpclib` should convert :class:`date` and :class:`time`
2214 instances.
2215
2216 .. Issue 1330538
2217
Christian Heimesf6cd9672008-03-26 13:45:42 +00002218* (3.0-warning mode) The :class:`Exception` class now warns
2219 when accessed using slicing or index access; having
2220 :class:`Exception` behave like a tuple is being phased out.
2221
2222* (3.0-warning mode) inequality comparisons between two dictionaries
Christian Heimes02781dc2008-03-21 01:11:52 +00002223 or two objects that don't implement comparison methods are reported
2224 as warnings. ``dict1 == dict2`` still works, but ``dict1 < dict2``
2225 is being phased out.
2226
2227 Comparisons between cells, which are an implementation detail of Python's
2228 scoping rules, also cause warnings because such comparisons are forbidden
2229 entirely in 3.0.
2230
Christian Heimes5b5e81c2007-12-31 16:14:33 +00002231.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +00002232
2233
2234.. _acks:
2235
2236Acknowledgements
2237================
2238
2239The author would like to thank the following people for offering suggestions,
2240corrections and assistance with various drafts of this article: .
2241