Georg Brandl | c47408a | 2008-12-04 18:44:53 +0000 | [diff] [blame] | 1 | **************************** |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 2 | What's New In Python 3.1 |
Georg Brandl | c47408a | 2008-12-04 18:44:53 +0000 | [diff] [blame] | 3 | **************************** |
| 4 | |
Raymond Hettinger | 1f251a0 | 2009-04-04 10:47:35 +0000 | [diff] [blame] | 5 | :Author: Raymond Hettinger |
Benjamin Peterson | 3432829 | 2008-12-05 03:05:29 +0000 | [diff] [blame] | 6 | :Release: |release| |
| 7 | :Date: |today| |
Georg Brandl | c47408a | 2008-12-04 18:44:53 +0000 | [diff] [blame] | 8 | |
| 9 | .. $Id$ |
| 10 | Rules for maintenance: |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 11 | |
Georg Brandl | c47408a | 2008-12-04 18:44:53 +0000 | [diff] [blame] | 12 | * Anyone can add text to this document. Do not spend very much time |
| 13 | on the wording of your changes, because your text will probably |
| 14 | get rewritten to some degree. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 15 | |
Georg Brandl | c47408a | 2008-12-04 18:44:53 +0000 | [diff] [blame] | 16 | * The maintainer will go through Misc/NEWS periodically and add |
| 17 | changes; it's therefore more important to add your changes to |
Raymond Hettinger | b9b3319 | 2009-04-08 09:38:32 +0000 | [diff] [blame] | 18 | Misc/NEWS than to this file. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 19 | |
Georg Brandl | c47408a | 2008-12-04 18:44:53 +0000 | [diff] [blame] | 20 | * This is not a complete list of every single change; completeness |
| 21 | is the purpose of Misc/NEWS. Some changes I consider too small |
| 22 | or esoteric to include. If such a change is added to the text, |
| 23 | I'll just remove it. (This is another reason you shouldn't spend |
| 24 | too much time on writing your addition.) |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 25 | |
Georg Brandl | c47408a | 2008-12-04 18:44:53 +0000 | [diff] [blame] | 26 | * If you want to draw your new text to the attention of the |
| 27 | maintainer, add 'XXX' to the beginning of the paragraph or |
| 28 | section. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 29 | |
Georg Brandl | c47408a | 2008-12-04 18:44:53 +0000 | [diff] [blame] | 30 | * It's OK to just add a fragmentary note about a change. For |
| 31 | example: "XXX Describe the transmogrify() function added to the |
| 32 | socket module." The maintainer will research the change and |
| 33 | write the necessary text. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 34 | |
Georg Brandl | c47408a | 2008-12-04 18:44:53 +0000 | [diff] [blame] | 35 | * You can comment out your additions if you like, but it's not |
| 36 | necessary (especially when a final release is some months away). |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 37 | |
Georg Brandl | c47408a | 2008-12-04 18:44:53 +0000 | [diff] [blame] | 38 | * Credit the author of a patch or bugfix. Just the name is |
Raymond Hettinger | b9b3319 | 2009-04-08 09:38:32 +0000 | [diff] [blame] | 39 | sufficient; the e-mail address isn't necessary. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 40 | |
Georg Brandl | c47408a | 2008-12-04 18:44:53 +0000 | [diff] [blame] | 41 | * It's helpful to add the bug/patch number as a comment: |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 42 | |
Georg Brandl | c47408a | 2008-12-04 18:44:53 +0000 | [diff] [blame] | 43 | % Patch 12345 |
| 44 | XXX Describe the transmogrify() function added to the socket |
| 45 | module. |
| 46 | (Contributed by P.Y. Developer.) |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 47 | |
Georg Brandl | c47408a | 2008-12-04 18:44:53 +0000 | [diff] [blame] | 48 | This saves the maintainer the effort of going through the SVN log |
Raymond Hettinger | b9b3319 | 2009-04-08 09:38:32 +0000 | [diff] [blame] | 49 | when researching a change. |
Georg Brandl | c47408a | 2008-12-04 18:44:53 +0000 | [diff] [blame] | 50 | |
| 51 | This article explains the new features in Python 3.1, compared to 3.0. |
| 52 | |
Georg Brandl | c47408a | 2008-12-04 18:44:53 +0000 | [diff] [blame] | 53 | |
Raymond Hettinger | 1f251a0 | 2009-04-04 10:47:35 +0000 | [diff] [blame] | 54 | PEP 372: Ordered Dictionaries |
| 55 | ============================= |
| 56 | |
| 57 | Regular Python dictionaries iterate over key/value pairs in arbitrary order. |
| 58 | Over the years, a number of authors have written alternative implementations |
| 59 | that remember the order that the keys were originally inserted. Based on |
Raymond Hettinger | d621dd7 | 2009-04-14 08:16:50 +0000 | [diff] [blame] | 60 | the experiences from those implementations, a new |
| 61 | :class:`collections.OrderedDict` class has been introduced. |
Raymond Hettinger | 1f251a0 | 2009-04-04 10:47:35 +0000 | [diff] [blame] | 62 | |
| 63 | The OrderedDict API is substantially the same as regular dictionaries |
| 64 | but will iterate over keys and values in a guaranteed order depending on |
| 65 | when a key was first inserted. If a new entry overwrites an existing entry, |
| 66 | the original insertion position is left unchanged. Deleting an entry and |
| 67 | reinserting it will move it to the end. |
| 68 | |
| 69 | The standard library now supports use of ordered dictionaries in several |
Raymond Hettinger | 7f5d746 | 2009-04-14 08:05:31 +0000 | [diff] [blame] | 70 | modules. The :mod:`configparser` module uses them by default. This lets |
Raymond Hettinger | 1f251a0 | 2009-04-04 10:47:35 +0000 | [diff] [blame] | 71 | configuration files be read, modified, and then written back in their original |
Raymond Hettinger | d621dd7 | 2009-04-14 08:16:50 +0000 | [diff] [blame] | 72 | order. The *_asdict()* method for :func:`collections.namedtuple` now |
Raymond Hettinger | c4f6d29 | 2009-04-04 12:35:58 +0000 | [diff] [blame] | 73 | returns an ordered dictionary with the values appearing in the same order as |
| 74 | the underlying tuple indicies. The :mod:`json` module is being built-out with |
| 75 | an *object_pairs_hook* to allow OrderedDicts to be built by the decoder. |
Raymond Hettinger | 347396a | 2009-04-07 23:10:59 +0000 | [diff] [blame] | 76 | Support was also added for third-party tools like `PyYAML <http://pyyaml.org/>`_. |
Raymond Hettinger | 1f251a0 | 2009-04-04 10:47:35 +0000 | [diff] [blame] | 77 | |
| 78 | .. seealso:: |
| 79 | |
| 80 | :pep:`372` - Ordered Dictionaries |
Raymond Hettinger | f84dfe5 | 2009-04-04 13:13:56 +0000 | [diff] [blame] | 81 | PEP written by Armin Ronacher and Raymond Hettinger. Implementation |
| 82 | written by Raymond Hettinger. |
Raymond Hettinger | 1f251a0 | 2009-04-04 10:47:35 +0000 | [diff] [blame] | 83 | |
Raymond Hettinger | b9b3319 | 2009-04-08 09:38:32 +0000 | [diff] [blame] | 84 | |
Raymond Hettinger | 1f251a0 | 2009-04-04 10:47:35 +0000 | [diff] [blame] | 85 | PEP 378: Format Specifier for Thousands Separator |
| 86 | ================================================= |
| 87 | |
Georg Brandl | c4a55fc | 2010-02-06 18:46:57 +0000 | [diff] [blame] | 88 | The built-in :func:`format` function and the :meth:`str.format` method use |
Raymond Hettinger | 1f251a0 | 2009-04-04 10:47:35 +0000 | [diff] [blame] | 89 | a mini-language that now includes a simple, non-locale aware way to format |
| 90 | a number with a thousands separator. That provides a way to humanize a |
| 91 | program's output, improving its professional appearance and readability:: |
| 92 | |
Raymond Hettinger | 0422e14 | 2009-04-17 18:58:06 +0000 | [diff] [blame] | 93 | >>> format(1234567, ',d') |
| 94 | '1,234,567' |
| 95 | >>> format(1234567.89, ',.2f') |
| 96 | '1,234,567.89' |
Raymond Hettinger | d48ed2e | 2009-05-14 22:48:19 +0000 | [diff] [blame] | 97 | >>> format(12345.6 + 8901234.12j, ',f') |
| 98 | '12,345.600000+8,901,234.120000j' |
Raymond Hettinger | 1f251a0 | 2009-04-04 10:47:35 +0000 | [diff] [blame] | 99 | >>> format(Decimal('1234567.89'), ',f') |
| 100 | '1,234,567.89' |
| 101 | |
Raymond Hettinger | d48ed2e | 2009-05-14 22:48:19 +0000 | [diff] [blame] | 102 | The supported types are :class:`int`, :class:`float`, :class:`complex` |
| 103 | and :class:`decimal.Decimal`. |
Raymond Hettinger | c548b6e | 2009-04-17 10:09:27 +0000 | [diff] [blame] | 104 | |
Raymond Hettinger | 1f251a0 | 2009-04-04 10:47:35 +0000 | [diff] [blame] | 105 | Discussions are underway about how to specify alternative separators |
Raymond Hettinger | c4f6d29 | 2009-04-04 12:35:58 +0000 | [diff] [blame] | 106 | like dots, spaces, apostrophes, or underscores. Locale-aware applications |
| 107 | should use the existing *n* format specifier which already has some support |
| 108 | for thousands separators. |
Raymond Hettinger | 1f251a0 | 2009-04-04 10:47:35 +0000 | [diff] [blame] | 109 | |
| 110 | .. seealso:: |
| 111 | |
| 112 | :pep:`378` - Format Specifier for Thousands Separator |
Raymond Hettinger | 55fc9ce | 2009-04-14 20:45:17 +0000 | [diff] [blame] | 113 | PEP written by Raymond Hettinger and implemented by Eric Smith and |
Raymond Hettinger | 1f251a0 | 2009-04-04 10:47:35 +0000 | [diff] [blame] | 114 | Mark Dickinson. |
| 115 | |
| 116 | |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 117 | Other Language Changes |
| 118 | ====================== |
| 119 | |
| 120 | Some smaller changes made to the core Python language are: |
| 121 | |
Andrew M. Kuchling | 7718e5e | 2010-02-22 15:24:43 +0000 | [diff] [blame] | 122 | * Directories and zip archives containing a :file:`__main__.py` |
| 123 | file can now be executed directly by passing their name to the |
| 124 | interpreter. The directory/zipfile is automatically inserted as the |
| 125 | first entry in sys.path. (Suggestion and initial patch by Andy Chu; |
| 126 | revised patch by Phillip J. Eby and Nick Coghlan; :issue:`1739468`.) |
| 127 | |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 128 | * The :func:`int` type gained a ``bit_length`` method that returns the |
| 129 | number of bits necessary to represent its argument in binary:: |
| 130 | |
| 131 | >>> n = 37 |
| 132 | >>> bin(37) |
| 133 | '0b100101' |
| 134 | >>> n.bit_length() |
| 135 | 6 |
| 136 | >>> n = 2**123-1 |
| 137 | >>> n.bit_length() |
| 138 | 123 |
| 139 | >>> (n+1).bit_length() |
| 140 | 124 |
| 141 | |
Raymond Hettinger | c4f6d29 | 2009-04-04 12:35:58 +0000 | [diff] [blame] | 142 | (Contributed by Fredrik Johansson, Victor Stinner, Raymond Hettinger, |
| 143 | and Mark Dickinson; :issue:`3439`.) |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 144 | |
Raymond Hettinger | 79d0b0e | 2009-04-07 07:11:00 +0000 | [diff] [blame] | 145 | * The fields in :func:`format` strings can now be automatically |
| 146 | numbered:: |
| 147 | |
| 148 | >>> 'Sir {} of {}'.format('Gallahad', 'Camelot') |
| 149 | 'Sir Gallahad of Camelot' |
| 150 | |
| 151 | Formerly, the string would have required numbered fields such as: |
| 152 | ``'Sir {0} of {1}'``. |
| 153 | |
| 154 | (Contributed by Eric Smith; :issue:`5237`.) |
| 155 | |
Raymond Hettinger | bd3da6b | 2009-05-15 16:16:12 +0000 | [diff] [blame] | 156 | * The :func:`string.maketrans` function is deprecated and is replaced by new |
| 157 | static methods, :meth:`bytes.maketrans` and :meth:`bytearray.maketrans`. |
| 158 | This change solves the confusion around which types were supported by the |
| 159 | :mod:`string` module. Now, :class:`str`, :class:`bytes`, and |
| 160 | :class:`bytearray` each have their own **maketrans** and **translate** |
| 161 | methods with intermediate translation tables of the appropriate type. |
| 162 | |
| 163 | (Contributed by Georg Brandl; :issue:`5675`.) |
| 164 | |
Raymond Hettinger | 81c0dce | 2009-05-27 09:12:18 +0000 | [diff] [blame] | 165 | * The syntax of the :keyword:`with` statement now allows multiple context |
| 166 | managers in a single statement:: |
| 167 | |
| 168 | >>> with open('mylog.txt') as infile, open('a.out', 'w') as outfile: |
| 169 | ... for line in infile: |
| 170 | ... if '<critical>' in line: |
| 171 | ... outfile.write(line) |
| 172 | |
Raymond Hettinger | 91e3b9d | 2009-05-28 22:20:03 +0000 | [diff] [blame] | 173 | With the new syntax, the :func:`contextlib.nested` function is no longer |
Raymond Hettinger | 323ee0c | 2009-05-28 22:49:33 +0000 | [diff] [blame] | 174 | needed and is now deprecated. |
Raymond Hettinger | 91e3b9d | 2009-05-28 22:20:03 +0000 | [diff] [blame] | 175 | |
Georg Brandl | ed23ab6 | 2009-05-27 19:46:38 +0000 | [diff] [blame] | 176 | (Contributed by Georg Brandl and Mattias Brändström; |
Raymond Hettinger | 81c0dce | 2009-05-27 09:12:18 +0000 | [diff] [blame] | 177 | `appspot issue 53094 <http://codereview.appspot.com/53094>`_.) |
| 178 | |
Raymond Hettinger | 79d0b0e | 2009-04-07 07:11:00 +0000 | [diff] [blame] | 179 | * ``round(x, n)`` now returns an integer if *x* is an integer. |
| 180 | Previously it returned a float:: |
| 181 | |
| 182 | >>> round(1123, -2) |
| 183 | 1100 |
| 184 | |
| 185 | (Contributed by Mark Dickinson; :issue:`4707`.) |
| 186 | |
Raymond Hettinger | 8648e50 | 2009-04-17 00:11:54 +0000 | [diff] [blame] | 187 | * Python now uses David Gay's algorithm for finding the shortest floating |
| 188 | point representation that doesn't change its value. This should help |
Eric Smith | 86c06bc | 2009-04-17 12:33:24 +0000 | [diff] [blame] | 189 | mitigate some of the confusion surrounding binary floating point |
Raymond Hettinger | 8648e50 | 2009-04-17 00:11:54 +0000 | [diff] [blame] | 190 | numbers. |
| 191 | |
| 192 | The significance is easily seen with a number like ``1.1`` which does not |
| 193 | have an exact equivalent in binary floating point. Since there is no exact |
Raymond Hettinger | fee346b | 2009-04-17 09:45:19 +0000 | [diff] [blame] | 194 | equivalent, an expression like ``float('1.1')`` evaluates to the nearest |
Raymond Hettinger | 8648e50 | 2009-04-17 00:11:54 +0000 | [diff] [blame] | 195 | representable value which is ``0x1.199999999999ap+0`` in hex or |
| 196 | ``1.100000000000000088817841970012523233890533447265625`` in decimal. That |
| 197 | nearest value was and still is used in subsequent floating point |
| 198 | calculations. |
| 199 | |
| 200 | What is new is how the number gets displayed. Formerly, Python used a |
| 201 | simple approach. The value of ``repr(1.1)`` was computed as ``format(1.1, |
Raymond Hettinger | fee346b | 2009-04-17 09:45:19 +0000 | [diff] [blame] | 202 | '.17g')`` which evaluated to ``'1.1000000000000001'``. The advantage of |
Raymond Hettinger | 8648e50 | 2009-04-17 00:11:54 +0000 | [diff] [blame] | 203 | using 17 digits was that it relied on IEEE-754 guarantees to assure that |
| 204 | ``eval(repr(1.1))`` would round-trip exactly to its original value. The |
| 205 | disadvantage is that many people found the output to be confusing (mistaking |
| 206 | intrinsic limitations of binary floating point representation as being a |
| 207 | problem with Python itself). |
| 208 | |
Raymond Hettinger | fee346b | 2009-04-17 09:45:19 +0000 | [diff] [blame] | 209 | The new algorithm for ``repr(1.1)`` is smarter and returns ``'1.1'``. |
Raymond Hettinger | 8648e50 | 2009-04-17 00:11:54 +0000 | [diff] [blame] | 210 | Effectively, it searches all equivalent string representations (ones that |
Raymond Hettinger | fee346b | 2009-04-17 09:45:19 +0000 | [diff] [blame] | 211 | get stored with the same underlying float value) and returns the shortest |
Raymond Hettinger | 8648e50 | 2009-04-17 00:11:54 +0000 | [diff] [blame] | 212 | representation. |
| 213 | |
| 214 | The new algorithm tends to emit cleaner representations when possible, but |
| 215 | it does not change the underlying values. So, it is still the case that |
| 216 | ``1.1 + 2.2 != 3.3`` even though the representations may suggest otherwise. |
| 217 | |
| 218 | The new algorithm depends on certain features in the underlying floating |
| 219 | point implementation. If the required features are not found, the old |
| 220 | algorithm will continue to be used. Also, the text pickle protocols |
| 221 | assure cross-platform portability by using the old algorithm. |
| 222 | |
| 223 | (Contributed by Eric Smith and Mark Dickinson; :issue:`1580`) |
Raymond Hettinger | 79d0b0e | 2009-04-07 07:11:00 +0000 | [diff] [blame] | 224 | |
| 225 | New, Improved, and Deprecated Modules |
| 226 | ===================================== |
| 227 | |
Raymond Hettinger | 1f251a0 | 2009-04-04 10:47:35 +0000 | [diff] [blame] | 228 | * Added a :class:`collections.Counter` class to support convenient |
| 229 | counting of unique items in a sequence or iterable:: |
| 230 | |
| 231 | >>> Counter(['red', 'blue', 'red', 'green', 'blue', 'blue']) |
| 232 | Counter({'blue': 3, 'red': 2, 'green': 1}) |
| 233 | |
| 234 | (Contributed by Raymond Hettinger; :issue:`1696199`.) |
| 235 | |
Raymond Hettinger | c1bd49a | 2009-04-06 23:11:08 +0000 | [diff] [blame] | 236 | * Added a new module, :mod:`tkinter.ttk` for access to the Tk themed widget set. |
Raymond Hettinger | dbd51b5 | 2009-04-06 22:45:52 +0000 | [diff] [blame] | 237 | The basic idea of ttk is to separate, to the extent possible, the code |
Raymond Hettinger | f84dfe5 | 2009-04-04 13:13:56 +0000 | [diff] [blame] | 238 | implementing a widget's behavior from the code implementing its appearance. |
| 239 | |
Raymond Hettinger | d1b3de3 | 2009-04-08 00:09:26 +0000 | [diff] [blame] | 240 | (Contributed by Guilherme Polo; :issue:`2983`.) |
Raymond Hettinger | f84dfe5 | 2009-04-04 13:13:56 +0000 | [diff] [blame] | 241 | |
Raymond Hettinger | f4cc2c4 | 2009-04-06 22:39:03 +0000 | [diff] [blame] | 242 | * The :class:`gzip.GzipFile` and :class:`bz2.BZ2File` classes now support |
| 243 | the context manager protocol:: |
Raymond Hettinger | 1f251a0 | 2009-04-04 10:47:35 +0000 | [diff] [blame] | 244 | |
Raymond Hettinger | f4cc2c4 | 2009-04-06 22:39:03 +0000 | [diff] [blame] | 245 | >>> # Automatically close file after writing |
| 246 | >>> with gzip.GzipFile(filename, "wb") as f: |
| 247 | ... f.write(b"xxx") |
| 248 | |
| 249 | (Contributed by Antoine Pitrou.) |
Raymond Hettinger | 1f251a0 | 2009-04-04 10:47:35 +0000 | [diff] [blame] | 250 | |
Raymond Hettinger | bf1537c | 2009-04-09 22:34:46 +0000 | [diff] [blame] | 251 | * The :mod:`decimal` module now supports methods for creating a |
Raymond Hettinger | 76efa2b | 2009-04-06 23:11:47 +0000 | [diff] [blame] | 252 | decimal object from a binary :class:`float`. The conversion is |
Raymond Hettinger | 1f251a0 | 2009-04-04 10:47:35 +0000 | [diff] [blame] | 253 | exact but can sometimes be surprising:: |
| 254 | |
| 255 | >>> Decimal.from_float(1.1) |
| 256 | Decimal('1.100000000000000088817841970012523233890533447265625') |
| 257 | |
| 258 | The long decimal result shows the actual binary fraction being |
| 259 | stored for *1.1*. The fraction has many digits because *1.1* cannot |
| 260 | be exactly represented in binary. |
| 261 | |
| 262 | (Contributed by Raymond Hettinger and Mark Dickinson.) |
| 263 | |
Raymond Hettinger | e7ec57d | 2009-04-04 11:08:48 +0000 | [diff] [blame] | 264 | * The :mod:`itertools` module grew two new functions. The |
| 265 | :func:`itertools.combinations_with_replacement` function is one of |
| 266 | four for generating combinatorics including permutations and Cartesian |
| 267 | products. The :func:`itertools.compress` function mimics its namesake |
| 268 | from APL. Also, the existing :func:`itertools.count` function now has |
| 269 | an optional *step* argument and can accept any type of counting |
| 270 | sequence including :class:`fractions.Fraction` and |
Raymond Hettinger | 8d97ccb | 2009-04-06 17:55:05 +0000 | [diff] [blame] | 271 | :class:`decimal.Decimal`:: |
| 272 | |
| 273 | >>> [p+q for p,q in combinations_with_replacement('LOVE', 2)] |
| 274 | ['LL', 'LO', 'LV', 'LE', 'OO', 'OV', 'OE', 'VV', 'VE', 'EE'] |
| 275 | |
| 276 | >>> list(compress(data=range(10), selectors=[0,0,1,1,0,1,0,1,0,0])) |
| 277 | [2, 3, 5, 7] |
| 278 | |
| 279 | >>> c = count(start=Fraction(1,2), step=Fraction(1,6)) |
Raymond Hettinger | bd3da6b | 2009-05-15 16:16:12 +0000 | [diff] [blame] | 280 | >>> [next(c), next(c), next(c), next(c)] |
| 281 | [Fraction(1, 2), Fraction(2, 3), Fraction(5, 6), Fraction(1, 1)] |
Raymond Hettinger | e7ec57d | 2009-04-04 11:08:48 +0000 | [diff] [blame] | 282 | |
| 283 | (Contributed by Raymond Hettinger.) |
| 284 | |
Raymond Hettinger | f41857e | 2009-04-04 11:59:00 +0000 | [diff] [blame] | 285 | * :func:`collections.namedtuple` now supports a keyword argument |
| 286 | *rename* which lets invalid fieldnames be automatically converted to |
| 287 | positional names in the form _0, _1, etc. This is useful when |
| 288 | the field names are being created by an external source such as a |
Raymond Hettinger | 4c0b1e4 | 2009-04-08 07:49:03 +0000 | [diff] [blame] | 289 | CSV header, SQL field list, or user input:: |
| 290 | |
Raymond Hettinger | 2a39e0f | 2009-04-08 22:50:09 +0000 | [diff] [blame] | 291 | >>> query = input() |
| 292 | SELECT region, dept, count(*) FROM main GROUPBY region, dept |
Raymond Hettinger | 4c0b1e4 | 2009-04-08 07:49:03 +0000 | [diff] [blame] | 293 | |
| 294 | >>> cursor.execute(query) |
| 295 | >>> query_fields = [desc[0] for desc in cursor.description] |
| 296 | >>> UserQuery = namedtuple('UserQuery', query_fields, rename=True) |
| 297 | >>> pprint.pprint([UserQuery(*row) for row in cursor]) |
| 298 | [UserQuery(region='South', dept='Shipping', _2=185), |
| 299 | UserQuery(region='North', dept='Accounting', _2=37), |
| 300 | UserQuery(region='West', dept='Sales', _2=419)] |
Raymond Hettinger | f41857e | 2009-04-04 11:59:00 +0000 | [diff] [blame] | 301 | |
| 302 | (Contributed by Raymond Hettinger; :issue:`1818`.) |
| 303 | |
Raymond Hettinger | f41857e | 2009-04-04 11:59:00 +0000 | [diff] [blame] | 304 | * The :func:`re.sub`, :func:`re.subn` and :func:`re.split` functions now |
| 305 | accept a flags parameter. |
| 306 | |
| 307 | (Contributed by Gregory Smith.) |
| 308 | |
Raymond Hettinger | d621dd7 | 2009-04-14 08:16:50 +0000 | [diff] [blame] | 309 | * The :mod:`logging` module now implements a simple :class:`logging.NullHandler` |
Raymond Hettinger | 35a8836 | 2009-04-09 00:08:24 +0000 | [diff] [blame] | 310 | class for applications that are not using logging but are calling |
| 311 | library code that does. Setting-up a null handler will suppress |
Raymond Hettinger | d621dd7 | 2009-04-14 08:16:50 +0000 | [diff] [blame] | 312 | spurious warnings such as "No handlers could be found for logger foo":: |
Raymond Hettinger | 35a8836 | 2009-04-09 00:08:24 +0000 | [diff] [blame] | 313 | |
| 314 | >>> h = logging.NullHandler() |
| 315 | >>> logging.getLogger("foo").addHandler(h) |
| 316 | |
Raymond Hettinger | d621dd7 | 2009-04-14 08:16:50 +0000 | [diff] [blame] | 317 | (Contributed by Vinay Sajip; :issue:`4384`). |
Raymond Hettinger | 35a8836 | 2009-04-09 00:08:24 +0000 | [diff] [blame] | 318 | |
Raymond Hettinger | f41857e | 2009-04-04 11:59:00 +0000 | [diff] [blame] | 319 | * The :mod:`runpy` module which supports the ``-m`` command line switch |
| 320 | now supports the execution of packages by looking for and executing |
| 321 | a ``__main__`` submodule when a package name is supplied. |
| 322 | |
| 323 | (Contributed by Andi Vajda; :issue:`4195`.) |
| 324 | |
| 325 | * The :mod:`pdb` module can now access and display source code loaded via |
| 326 | :mod:`zipimport` (or any other conformant :pep:`302` loader). |
| 327 | |
| 328 | (Contributed by Alexander Belopolsky; :issue:`4201`.) |
| 329 | |
| 330 | * :class:`functools.partial` objects can now be pickled. |
| 331 | |
| 332 | (Suggested by Antoine Pitrou and Jesse Noller. Implemented by |
Georg Brandl | 6faee4e | 2010-09-21 14:48:28 +0000 | [diff] [blame] | 333 | Jack Diederich; :issue:`5228`.) |
Raymond Hettinger | f41857e | 2009-04-04 11:59:00 +0000 | [diff] [blame] | 334 | |
Raymond Hettinger | 8e33051 | 2009-04-04 13:20:55 +0000 | [diff] [blame] | 335 | * Add :mod:`pydoc` help topics for symbols so that ``help('@')`` |
| 336 | works as expected in the interactive environment. |
| 337 | |
| 338 | (Contributed by David Laban; :issue:`4739`.) |
| 339 | |
Raymond Hettinger | c4f6d29 | 2009-04-04 12:35:58 +0000 | [diff] [blame] | 340 | * The :mod:`unittest` module now supports skipping individual tests or classes |
| 341 | of tests. And it supports marking a test as a expected failure, a test that |
Raymond Hettinger | 8daab40 | 2009-04-04 13:01:19 +0000 | [diff] [blame] | 342 | is known to be broken, but shouldn't be counted as a failure on a |
Raymond Hettinger | 8d97ccb | 2009-04-06 17:55:05 +0000 | [diff] [blame] | 343 | TestResult:: |
| 344 | |
| 345 | class TestGizmo(unittest.TestCase): |
| 346 | |
| 347 | @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows") |
| 348 | def test_gizmo_on_windows(self): |
| 349 | ... |
| 350 | |
| 351 | @unittest.expectedFailure |
| 352 | def test_gimzo_without_required_library(self): |
| 353 | ... |
Raymond Hettinger | f41857e | 2009-04-04 11:59:00 +0000 | [diff] [blame] | 354 | |
Raymond Hettinger | 55fc9ce | 2009-04-14 20:45:17 +0000 | [diff] [blame] | 355 | Also, tests for exceptions have been builtout to work with context managers |
| 356 | using the :keyword:`with` statement:: |
Raymond Hettinger | 35a8836 | 2009-04-09 00:08:24 +0000 | [diff] [blame] | 357 | |
| 358 | def test_division_by_zero(self): |
| 359 | with self.assertRaises(ZeroDivisionError): |
| 360 | x / 0 |
| 361 | |
| 362 | In addition, several new assertion methods were added including |
| 363 | :func:`assertSetEqual`, :func:`assertDictEqual`, |
| 364 | :func:`assertDictContainsSubset`, :func:`assertListEqual`, |
| 365 | :func:`assertTupleEqual`, :func:`assertSequenceEqual`, |
| 366 | :func:`assertRaisesRegexp`, :func:`assertIsNone`, |
Michael Foord | 5859b86 | 2009-04-25 20:47:43 +0000 | [diff] [blame] | 367 | and :func:`assertIsNotNone`. |
Raymond Hettinger | 35a8836 | 2009-04-09 00:08:24 +0000 | [diff] [blame] | 368 | |
| 369 | (Contributed by Benjamin Peterson and Antoine Pitrou.) |
| 370 | |
Raymond Hettinger | be3a821 | 2009-04-09 00:18:29 +0000 | [diff] [blame] | 371 | * The :mod:`io` module has three new constants for the :meth:`seek` |
Raymond Hettinger | 35a8836 | 2009-04-09 00:08:24 +0000 | [diff] [blame] | 372 | method :data:`SEEK_SET`, :data:`SEEK_CUR`, and :data:`SEEK_END`. |
| 373 | |
| 374 | * The :attr:`sys.version_info` tuple is now a named tuple:: |
| 375 | |
| 376 | >>> sys.version_info |
| 377 | sys.version_info(major=3, minor=1, micro=0, releaselevel='alpha', serial=2) |
| 378 | |
| 379 | (Contributed by Ross Light; :issue:`4285`.) |
Raymond Hettinger | c4f6d29 | 2009-04-04 12:35:58 +0000 | [diff] [blame] | 380 | |
Raymond Hettinger | daafea3 | 2009-05-15 15:21:33 +0000 | [diff] [blame] | 381 | * The :mod:`nntplib` and :mod:`imaplib` modules now support IPv6. |
Raymond Hettinger | 9dd7476 | 2009-05-15 14:57:35 +0000 | [diff] [blame] | 382 | |
| 383 | (Contributed by Derek Morr; :issue:`1655` and :issue:`1664`.) |
| 384 | |
Raymond Hettinger | 662bad8 | 2009-06-09 21:01:05 +0000 | [diff] [blame] | 385 | * The :mod:`pickle` module has been adapted for better interoperability with |
| 386 | Python 2.x when used with protocol 2 or lower. The reorganization of the |
| 387 | standard library changed the formal reference for many objects. For |
| 388 | example, ``__builtin__.set`` in Python 2 is called ``builtins.set`` in Python |
Raymond Hettinger | 5c96308 | 2009-06-10 15:38:52 +0000 | [diff] [blame] | 389 | 3. This change confounded efforts to share data between different versions of |
Raymond Hettinger | 662bad8 | 2009-06-09 21:01:05 +0000 | [diff] [blame] | 390 | Python. But now when protocol 2 or lower is selected, the pickler will |
| 391 | automatically use the old Python 2 names for both loading and dumping. This |
| 392 | remapping is turned-on by default but can be disabled with the *fix_imports* |
| 393 | option:: |
| 394 | |
| 395 | >>> s = {1, 2, 3} |
| 396 | >>> pickle.dumps(s, protocol=0) |
| 397 | b'c__builtin__\nset\np0\n((lp1\nL1L\naL2L\naL3L\natp2\nRp3\n.' |
| 398 | >>> pickle.dumps(s, protocol=0, fix_imports=False) |
| 399 | b'cbuiltins\nset\np0\n((lp1\nL1L\naL2L\naL3L\natp2\nRp3\n.' |
| 400 | |
| 401 | An unfortunate but unavoidable side-effect of this change is that protocol 2 |
| 402 | pickles produced by Python 3.1 won't be readable with Python 3.0. The latest |
| 403 | pickle protocol, protocol 3, should be used when migrating data between |
| 404 | Python 3.x implementations, as it doesn't attempt to remain compatible with |
| 405 | Python 2.x. |
| 406 | |
| 407 | (Contributed by Alexandre Vassalotti and Antoine Pitrou, :issue:`6137`.) |
| 408 | |
Raymond Hettinger | c4f6d29 | 2009-04-04 12:35:58 +0000 | [diff] [blame] | 409 | * A new module, :mod:`importlib` was added. It provides a complete, portable, |
Raymond Hettinger | 55fc9ce | 2009-04-14 20:45:17 +0000 | [diff] [blame] | 410 | pure Python reference implementation of the :keyword:`import` statement and its |
Benjamin Peterson | 3fa0fb4 | 2009-04-04 12:42:53 +0000 | [diff] [blame] | 411 | counterpart, the :func:`__import__` function. It represents a substantial |
Raymond Hettinger | c4f6d29 | 2009-04-04 12:35:58 +0000 | [diff] [blame] | 412 | step forward in documenting and defining the actions that take place during |
| 413 | imports. |
| 414 | |
| 415 | (Contributed by Brett Cannon.) |
Raymond Hettinger | 1f251a0 | 2009-04-04 10:47:35 +0000 | [diff] [blame] | 416 | |
Antoine Pitrou | b556452 | 2009-03-28 19:45:26 +0000 | [diff] [blame] | 417 | Optimizations |
Raymond Hettinger | 79d0b0e | 2009-04-07 07:11:00 +0000 | [diff] [blame] | 418 | ============= |
Antoine Pitrou | b556452 | 2009-03-28 19:45:26 +0000 | [diff] [blame] | 419 | |
| 420 | Major performance enhancements have been added: |
| 421 | |
| 422 | * The new I/O library (as defined in :pep:`3116`) was mostly written in |
| 423 | Python and quickly proved to be a problematic bottleneck in Python 3.0. |
| 424 | In Python 3.1, the I/O library has been entirely rewritten in C and is |
| 425 | 2 to 20 times faster depending on the task at hand. The pure Python |
| 426 | version is still available for experimentation purposes through |
| 427 | the ``_pyio`` module. |
| 428 | |
| 429 | (Contributed by Amaury Forgeot d'Arc and Antoine Pitrou.) |
| 430 | |
Raymond Hettinger | 8daab40 | 2009-04-04 13:01:19 +0000 | [diff] [blame] | 431 | * Added a heuristic so that tuples and dicts containing only untrackable objects |
Raymond Hettinger | e7ec57d | 2009-04-04 11:08:48 +0000 | [diff] [blame] | 432 | are not tracked by the garbage collector. This can reduce the size of |
| 433 | collections and therefore the garbage collection overhead on long-running |
| 434 | programs, depending on their particular use of datatypes. |
| 435 | |
| 436 | (Contributed by Antoine Pitrou, :issue:`4688`.) |
| 437 | |
Raymond Hettinger | f41857e | 2009-04-04 11:59:00 +0000 | [diff] [blame] | 438 | * Enabling a configure option named ``--with-computed-gotos`` |
| 439 | on compilers that support it (notably: gcc, SunPro, icc), the bytecode |
| 440 | evaluation loop is compiled with a new dispatch mechanism which gives |
Raymond Hettinger | 2a02777 | 2009-04-04 12:46:57 +0000 | [diff] [blame] | 441 | speedups of up to 20%, depending on the system, the compiler, and |
| 442 | the benchmark. |
Raymond Hettinger | f41857e | 2009-04-04 11:59:00 +0000 | [diff] [blame] | 443 | |
Raymond Hettinger | 2a02777 | 2009-04-04 12:46:57 +0000 | [diff] [blame] | 444 | (Contributed by Antoine Pitrou along with a number of other participants, |
| 445 | :issue:`4753`). |
Raymond Hettinger | f41857e | 2009-04-04 11:59:00 +0000 | [diff] [blame] | 446 | |
| 447 | * The decoding of UTF-8, UTF-16 and LATIN-1 is now two to four times |
| 448 | faster. |
| 449 | |
| 450 | (Contributed by Antoine Pitrou and Amaury Forgeot d'Arc, :issue:`4868`.) |
| 451 | |
Raymond Hettinger | bd3da6b | 2009-05-15 16:16:12 +0000 | [diff] [blame] | 452 | * The :mod:`json` module now has a C extension to substantially improve |
Raymond Hettinger | d48ed2e | 2009-05-14 22:48:19 +0000 | [diff] [blame] | 453 | its performance. In addition, the API was modified so that json works |
| 454 | only with :class:`str`, not with :class:`bytes`. That change makes the |
Raymond Hettinger | bd3da6b | 2009-05-15 16:16:12 +0000 | [diff] [blame] | 455 | module closely match the `JSON specification <http://json.org/>`_ |
Raymond Hettinger | d48ed2e | 2009-05-14 22:48:19 +0000 | [diff] [blame] | 456 | which is defined in terms of Unicode. |
Raymond Hettinger | 9dd7476 | 2009-05-15 14:57:35 +0000 | [diff] [blame] | 457 | |
Raymond Hettinger | d48ed2e | 2009-05-14 22:48:19 +0000 | [diff] [blame] | 458 | (Contributed by Bob Ippolito and converted to Py3.1 by Antoine Pitrou |
| 459 | and Benjamin Peterson; :issue:`4136`.) |
Raymond Hettinger | c4f6d29 | 2009-04-04 12:35:58 +0000 | [diff] [blame] | 460 | |
Raymond Hettinger | d48ed2e | 2009-05-14 22:48:19 +0000 | [diff] [blame] | 461 | * Unpickling now interns the attribute names of pickled objects. This saves |
| 462 | memory and allows pickles to be smaller. |
Raymond Hettinger | 9dd7476 | 2009-05-15 14:57:35 +0000 | [diff] [blame] | 463 | |
Raymond Hettinger | d48ed2e | 2009-05-14 22:48:19 +0000 | [diff] [blame] | 464 | (Contributed by Jake McGuire and Antoine Pitrou; :issue:`5084`.) |
Raymond Hettinger | 35a8836 | 2009-04-09 00:08:24 +0000 | [diff] [blame] | 465 | |
Raymond Hettinger | eb81695 | 2009-05-29 01:22:18 +0000 | [diff] [blame] | 466 | IDLE |
| 467 | ==== |
| 468 | |
| 469 | * IDLE's format menu now provides an option to strip trailing whitespace |
| 470 | from a source file. |
| 471 | |
| 472 | (Contributed by Roger D. Serwy; :issue:`5150`.) |
| 473 | |
Raymond Hettinger | 35a8836 | 2009-04-09 00:08:24 +0000 | [diff] [blame] | 474 | Build and C API Changes |
| 475 | ======================= |
| 476 | |
| 477 | Changes to Python's build process and to the C API include: |
Raymond Hettinger | c4f6d29 | 2009-04-04 12:35:58 +0000 | [diff] [blame] | 478 | |
| 479 | * Integers are now stored internally either in base 2**15 or in base |
| 480 | 2**30, the base being determined at build time. Previously, they |
| 481 | were always stored in base 2**15. Using base 2**30 gives |
| 482 | significant performance improvements on 64-bit machines, but |
| 483 | benchmark results on 32-bit machines have been mixed. Therefore, |
| 484 | the default is to use base 2**30 on 64-bit machines and base 2**15 |
| 485 | on 32-bit machines; on Unix, there's a new configure option |
| 486 | ``--enable-big-digits`` that can be used to override this default. |
| 487 | |
| 488 | Apart from the performance improvements this change should be invisible to |
| 489 | end users, with one exception: for testing and debugging purposes there's a |
Raymond Hettinger | 79d0b0e | 2009-04-07 07:11:00 +0000 | [diff] [blame] | 490 | new :attr:`sys.int_info` that provides information about the |
Raymond Hettinger | c4f6d29 | 2009-04-04 12:35:58 +0000 | [diff] [blame] | 491 | internal format, giving the number of bits per digit and the size in bytes |
| 492 | of the C type used to store each digit:: |
| 493 | |
| 494 | >>> import sys |
| 495 | >>> sys.int_info |
| 496 | sys.int_info(bits_per_digit=30, sizeof_digit=4) |
| 497 | |
| 498 | (Contributed by Mark Dickinson; :issue:`4258`.) |
Antoine Pitrou | b556452 | 2009-03-28 19:45:26 +0000 | [diff] [blame] | 499 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 500 | * The :c:func:`PyLong_AsUnsignedLongLong()` function now handles a negative |
Raymond Hettinger | 35a8836 | 2009-04-09 00:08:24 +0000 | [diff] [blame] | 501 | *pylong* by raising :exc:`OverflowError` instead of :exc:`TypeError`. |
| 502 | |
| 503 | (Contributed by Mark Dickinson and Lisandro Dalcrin; :issue:`5175`.) |
| 504 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 505 | * Deprecated :c:func:`PyNumber_Int`. Use :c:func:`PyNumber_Long` instead. |
Raymond Hettinger | 35a8836 | 2009-04-09 00:08:24 +0000 | [diff] [blame] | 506 | |
Raymond Hettinger | be3a821 | 2009-04-09 00:18:29 +0000 | [diff] [blame] | 507 | (Contributed by Mark Dickinson; :issue:`4910`.) |
Raymond Hettinger | 1a6b73d | 2009-04-17 20:55:52 +0000 | [diff] [blame] | 508 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 509 | * Added a new :c:func:`PyOS_string_to_double` function to replace the |
| 510 | deprecated functions :c:func:`PyOS_ascii_strtod` and :c:func:`PyOS_ascii_atof`. |
Raymond Hettinger | bd3da6b | 2009-05-15 16:16:12 +0000 | [diff] [blame] | 511 | |
| 512 | (Contributed by Mark Dickinson; :issue:`5914`.) |
| 513 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 514 | * Added :c:type:`PyCapsule` as a replacement for the :c:type:`PyCObject` API. |
Raymond Hettinger | 4f20a74 | 2009-05-14 23:17:38 +0000 | [diff] [blame] | 515 | The principal difference is that the new type has a well defined interface |
| 516 | for passing typing safety information and a less complicated signature |
Raymond Hettinger | 9dd7476 | 2009-05-15 14:57:35 +0000 | [diff] [blame] | 517 | for calling a destructor. The old type had a problematic API and is now |
| 518 | deprecated. |
Raymond Hettinger | 4f20a74 | 2009-05-14 23:17:38 +0000 | [diff] [blame] | 519 | |
| 520 | (Contributed by Larry Hastings; :issue:`5630`.) |
| 521 | |
Raymond Hettinger | 1a6b73d | 2009-04-17 20:55:52 +0000 | [diff] [blame] | 522 | Porting to Python 3.1 |
| 523 | ===================== |
| 524 | |
| 525 | This section lists previously described changes and other bugfixes |
| 526 | that may require changes to your code: |
| 527 | |
| 528 | * The new floating point string representations can break existing doctests. |
| 529 | For example:: |
| 530 | |
| 531 | def e(): |
| 532 | '''Compute the base of natural logarithms. |
| 533 | |
| 534 | >>> e() |
| 535 | 2.7182818284590451 |
| 536 | |
| 537 | ''' |
| 538 | return sum(1/math.factorial(x) for x in reversed(range(30))) |
| 539 | |
| 540 | doctest.testmod() |
| 541 | |
| 542 | ********************************************************************** |
| 543 | Failed example: |
| 544 | e() |
| 545 | Expected: |
| 546 | 2.7182818284590451 |
| 547 | Got: |
| 548 | 2.718281828459045 |
| 549 | ********************************************************************** |
Raymond Hettinger | 5b4a54c | 2009-06-09 21:07:46 +0000 | [diff] [blame] | 550 | |
| 551 | * The automatic name remapping in the pickle module for protocol 2 or lower can |
| 552 | make Python 3.1 pickles unreadable in Python 3.0. One solution is to use |
| 553 | protocol 3. Another solution is to set the *fix_imports* option to **False**. |
| 554 | See the discussion above for more details. |