blob: f4ffd762739885c957877896399f27a99103e80e [file] [log] [blame]
Georg Brandlc47408a2008-12-04 18:44:53 +00001****************************
Georg Brandl48310cd2009-01-03 21:18:54 +00002 What's New In Python 3.1
Georg Brandlc47408a2008-12-04 18:44:53 +00003****************************
4
5.. XXX Add trademark info for Apple, Microsoft.
6
Raymond Hettinger1f251a02009-04-04 10:47:35 +00007:Author: Raymond Hettinger
Benjamin Peterson34328292008-12-05 03:05:29 +00008:Release: |release|
9:Date: |today|
Georg Brandlc47408a2008-12-04 18:44:53 +000010
11.. $Id$
12 Rules for maintenance:
Georg Brandl48310cd2009-01-03 21:18:54 +000013
Georg Brandlc47408a2008-12-04 18:44:53 +000014 * 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.
Georg Brandl48310cd2009-01-03 21:18:54 +000017
Georg Brandlc47408a2008-12-04 18:44:53 +000018 * 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. (Note: I didn't get to this for 3.0.
21 GvR.)
Georg Brandl48310cd2009-01-03 21:18:54 +000022
Georg Brandlc47408a2008-12-04 18:44:53 +000023 * This is not a complete list of every single change; completeness
24 is the purpose of Misc/NEWS. Some changes I consider too small
25 or esoteric to include. If such a change is added to the text,
26 I'll just remove it. (This is another reason you shouldn't spend
27 too much time on writing your addition.)
Georg Brandl48310cd2009-01-03 21:18:54 +000028
Georg Brandlc47408a2008-12-04 18:44:53 +000029 * If you want to draw your new text to the attention of the
30 maintainer, add 'XXX' to the beginning of the paragraph or
31 section.
Georg Brandl48310cd2009-01-03 21:18:54 +000032
Georg Brandlc47408a2008-12-04 18:44:53 +000033 * It's OK to just add a fragmentary note about a change. For
34 example: "XXX Describe the transmogrify() function added to the
35 socket module." The maintainer will research the change and
36 write the necessary text.
Georg Brandl48310cd2009-01-03 21:18:54 +000037
Georg Brandlc47408a2008-12-04 18:44:53 +000038 * You can comment out your additions if you like, but it's not
39 necessary (especially when a final release is some months away).
Georg Brandl48310cd2009-01-03 21:18:54 +000040
Georg Brandlc47408a2008-12-04 18:44:53 +000041 * Credit the author of a patch or bugfix. Just the name is
42 sufficient; the e-mail address isn't necessary. (Due to time
43 constraints I haven't managed to do this for 3.0. GvR.)
Georg Brandl48310cd2009-01-03 21:18:54 +000044
Georg Brandlc47408a2008-12-04 18:44:53 +000045 * It's helpful to add the bug/patch number as a comment:
Georg Brandl48310cd2009-01-03 21:18:54 +000046
Georg Brandlc47408a2008-12-04 18:44:53 +000047 % Patch 12345
48 XXX Describe the transmogrify() function added to the socket
49 module.
50 (Contributed by P.Y. Developer.)
Georg Brandl48310cd2009-01-03 21:18:54 +000051
Georg Brandlc47408a2008-12-04 18:44:53 +000052 This saves the maintainer the effort of going through the SVN log
53 when researching a change. (Again, I didn't get to this for 3.0.
54 GvR.)
55
56This article explains the new features in Python 3.1, compared to 3.0.
57
58.. Compare with previous release in 2 - 3 sentences here.
59.. add hyperlink when the documentation becomes available online.
60
61.. ======================================================================
62.. Large, PEP-level features and changes should be described here.
63.. Should there be a new section here for 3k migration?
64.. Or perhaps a more general section describing module changes/deprecation?
65.. sets module deprecated
66.. ======================================================================
67
68
Raymond Hettinger1f251a02009-04-04 10:47:35 +000069PEP 372: Ordered Dictionaries
70=============================
71
72Regular Python dictionaries iterate over key/value pairs in arbitrary order.
73Over the years, a number of authors have written alternative implementations
74that remember the order that the keys were originally inserted. Based on
75the experiences from those implementations, the :mod:`collections` module
76now has an :class:`OrderedDict` class.
77
78The OrderedDict API is substantially the same as regular dictionaries
79but will iterate over keys and values in a guaranteed order depending on
80when a key was first inserted. If a new entry overwrites an existing entry,
81the original insertion position is left unchanged. Deleting an entry and
82reinserting it will move it to the end.
83
84The standard library now supports use of ordered dictionaries in several
Raymond Hettingerc4f6d292009-04-04 12:35:58 +000085modules. The :mod:`ConfigParser` module uses them by default. This lets
Raymond Hettinger1f251a02009-04-04 10:47:35 +000086configuration files be read, modified, and then written back in their original
87order. The :mod:`collections` module's :meth:`namedtuple._asdict` method now
Raymond Hettingerc4f6d292009-04-04 12:35:58 +000088returns an ordered dictionary with the values appearing in the same order as
89the underlying tuple indicies. The :mod:`json` module is being built-out with
90an *object_pairs_hook* to allow OrderedDicts to be built by the decoder.
Raymond Hettinger347396a2009-04-07 23:10:59 +000091Support was also added for third-party tools like `PyYAML <http://pyyaml.org/>`_.
Raymond Hettinger1f251a02009-04-04 10:47:35 +000092
93.. seealso::
94
95 :pep:`372` - Ordered Dictionaries
Raymond Hettingerf84dfe52009-04-04 13:13:56 +000096 PEP written by Armin Ronacher and Raymond Hettinger. Implementation
97 written by Raymond Hettinger.
Raymond Hettinger1f251a02009-04-04 10:47:35 +000098
99PEP 378: Format Specifier for Thousands Separator
100=================================================
101
102The builtin :func:`format` function and the :meth:`str.format` method use
103a mini-language that now includes a simple, non-locale aware way to format
104a number with a thousands separator. That provides a way to humanize a
105program's output, improving its professional appearance and readability::
106
107 >>> format(Decimal('1234567.89'), ',f')
108 '1,234,567.89'
109
Raymond Hettingerc4f6d292009-04-04 12:35:58 +0000110The currently supported types are :class:`int` and :class:`decimal.Decimal`.
Raymond Hettinger1f251a02009-04-04 10:47:35 +0000111Support for :class:`float` is expected before the beta release.
112Discussions are underway about how to specify alternative separators
Raymond Hettingerc4f6d292009-04-04 12:35:58 +0000113like dots, spaces, apostrophes, or underscores. Locale-aware applications
114should use the existing *n* format specifier which already has some support
115for thousands separators.
Raymond Hettinger1f251a02009-04-04 10:47:35 +0000116
117.. seealso::
118
119 :pep:`378` - Format Specifier for Thousands Separator
120 PEP written by Raymond Hettinger; implemented by Eric Smith and
121 Mark Dickinson.
122
123
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000124Other Language Changes
125======================
126
127Some smaller changes made to the core Python language are:
128
129* The :func:`int` type gained a ``bit_length`` method that returns the
130 number of bits necessary to represent its argument in binary::
131
132 >>> n = 37
133 >>> bin(37)
134 '0b100101'
135 >>> n.bit_length()
136 6
137 >>> n = 2**123-1
138 >>> n.bit_length()
139 123
140 >>> (n+1).bit_length()
141 124
142
Raymond Hettingerc4f6d292009-04-04 12:35:58 +0000143 (Contributed by Fredrik Johansson, Victor Stinner, Raymond Hettinger,
144 and Mark Dickinson; :issue:`3439`.)
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000145
Raymond Hettinger79d0b0e2009-04-07 07:11:00 +0000146* The fields in :func:`format` strings can now be automatically
147 numbered::
148
149 >>> 'Sir {} of {}'.format('Gallahad', 'Camelot')
150 'Sir Gallahad of Camelot'
151
152 Formerly, the string would have required numbered fields such as:
153 ``'Sir {0} of {1}'``.
154
155 (Contributed by Eric Smith; :issue:`5237`.)
156
157* ``round(x, n)`` now returns an integer if *x* is an integer.
158 Previously it returned a float::
159
160 >>> round(1123, -2)
161 1100
162
163 (Contributed by Mark Dickinson; :issue:`4707`.)
164
165.. ======================================================================
166
167New, Improved, and Deprecated Modules
168=====================================
169
Raymond Hettinger1f251a02009-04-04 10:47:35 +0000170* Added a :class:`collections.Counter` class to support convenient
171 counting of unique items in a sequence or iterable::
172
173 >>> Counter(['red', 'blue', 'red', 'green', 'blue', 'blue'])
174 Counter({'blue': 3, 'red': 2, 'green': 1})
175
176 (Contributed by Raymond Hettinger; :issue:`1696199`.)
177
Raymond Hettingerc1bd49a2009-04-06 23:11:08 +0000178* Added a new module, :mod:`tkinter.ttk` for access to the Tk themed widget set.
Raymond Hettingerdbd51b52009-04-06 22:45:52 +0000179 The basic idea of ttk is to separate, to the extent possible, the code
Raymond Hettingerf84dfe52009-04-04 13:13:56 +0000180 implementing a widget's behavior from the code implementing its appearance.
181
Raymond Hettingerd1b3de32009-04-08 00:09:26 +0000182 (Contributed by Guilherme Polo; :issue:`2983`.)
Raymond Hettingerf84dfe52009-04-04 13:13:56 +0000183
Raymond Hettingerf4cc2c42009-04-06 22:39:03 +0000184* The :class:`gzip.GzipFile` and :class:`bz2.BZ2File` classes now support
185 the context manager protocol::
Raymond Hettinger1f251a02009-04-04 10:47:35 +0000186
Raymond Hettingerf4cc2c42009-04-06 22:39:03 +0000187 >>> # Automatically close file after writing
188 >>> with gzip.GzipFile(filename, "wb") as f:
189 ... f.write(b"xxx")
190
191 (Contributed by Antoine Pitrou.)
Raymond Hettinger1f251a02009-04-04 10:47:35 +0000192
Raymond Hettinger76efa2b2009-04-06 23:11:47 +0000193* The :mod:`decimal.Decimal` module now supports methods for creating a
194 decimal object from a binary :class:`float`. The conversion is
Raymond Hettinger1f251a02009-04-04 10:47:35 +0000195 exact but can sometimes be surprising::
196
197 >>> Decimal.from_float(1.1)
198 Decimal('1.100000000000000088817841970012523233890533447265625')
199
200 The long decimal result shows the actual binary fraction being
201 stored for *1.1*. The fraction has many digits because *1.1* cannot
202 be exactly represented in binary.
203
204 (Contributed by Raymond Hettinger and Mark Dickinson.)
205
Raymond Hettingere7ec57d2009-04-04 11:08:48 +0000206* The :mod:`itertools` module grew two new functions. The
207 :func:`itertools.combinations_with_replacement` function is one of
208 four for generating combinatorics including permutations and Cartesian
209 products. The :func:`itertools.compress` function mimics its namesake
210 from APL. Also, the existing :func:`itertools.count` function now has
211 an optional *step* argument and can accept any type of counting
212 sequence including :class:`fractions.Fraction` and
Raymond Hettinger8d97ccb2009-04-06 17:55:05 +0000213 :class:`decimal.Decimal`::
214
215 >>> [p+q for p,q in combinations_with_replacement('LOVE', 2)]
216 ['LL', 'LO', 'LV', 'LE', 'OO', 'OV', 'OE', 'VV', 'VE', 'EE']
217
218 >>> list(compress(data=range(10), selectors=[0,0,1,1,0,1,0,1,0,0]))
219 [2, 3, 5, 7]
220
221 >>> c = count(start=Fraction(1,2), step=Fraction(1,6))
222 >>> next(c), next(c), next(c), next(c)
223 (Fraction(1, 2), Fraction(2, 3), Fraction(5, 6), Fraction(1, 1))
Raymond Hettingere7ec57d2009-04-04 11:08:48 +0000224
225 (Contributed by Raymond Hettinger.)
226
Raymond Hettingerf41857e2009-04-04 11:59:00 +0000227* :func:`collections.namedtuple` now supports a keyword argument
228 *rename* which lets invalid fieldnames be automatically converted to
229 positional names in the form _0, _1, etc. This is useful when
230 the field names are being created by an external source such as a
231 CSV header, SQL field list, or user input.
232
233 (Contributed by Raymond Hettinger; :issue:`1818`.)
234
Raymond Hettingerf41857e2009-04-04 11:59:00 +0000235* The :func:`re.sub`, :func:`re.subn` and :func:`re.split` functions now
236 accept a flags parameter.
237
238 (Contributed by Gregory Smith.)
239
240* The :mod:`runpy` module which supports the ``-m`` command line switch
241 now supports the execution of packages by looking for and executing
242 a ``__main__`` submodule when a package name is supplied.
243
244 (Contributed by Andi Vajda; :issue:`4195`.)
245
246* The :mod:`pdb` module can now access and display source code loaded via
247 :mod:`zipimport` (or any other conformant :pep:`302` loader).
248
249 (Contributed by Alexander Belopolsky; :issue:`4201`.)
250
251* :class:`functools.partial` objects can now be pickled.
252
253 (Suggested by Antoine Pitrou and Jesse Noller. Implemented by
254 Jack Diedrich; :issue:`5228`.)
255
Raymond Hettinger8e330512009-04-04 13:20:55 +0000256* Add :mod:`pydoc` help topics for symbols so that ``help('@')``
257 works as expected in the interactive environment.
258
259 (Contributed by David Laban; :issue:`4739`.)
260
Raymond Hettingerc4f6d292009-04-04 12:35:58 +0000261* The :mod:`unittest` module now supports skipping individual tests or classes
262 of tests. And it supports marking a test as a expected failure, a test that
Raymond Hettinger8daab402009-04-04 13:01:19 +0000263 is known to be broken, but shouldn't be counted as a failure on a
Raymond Hettinger8d97ccb2009-04-06 17:55:05 +0000264 TestResult::
265
266 class TestGizmo(unittest.TestCase):
267
268 @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
269 def test_gizmo_on_windows(self):
270 ...
271
272 @unittest.expectedFailure
273 def test_gimzo_without_required_library(self):
274 ...
Raymond Hettingerf41857e2009-04-04 11:59:00 +0000275
Raymond Hettingerc4f6d292009-04-04 12:35:58 +0000276 (Contributed by Benjamin Peterson.)
277
278* A new module, :mod:`importlib` was added. It provides a complete, portable,
279 pure Python reference implementation of the *import* statement and its
Benjamin Peterson3fa0fb42009-04-04 12:42:53 +0000280 counterpart, the :func:`__import__` function. It represents a substantial
Raymond Hettingerc4f6d292009-04-04 12:35:58 +0000281 step forward in documenting and defining the actions that take place during
282 imports.
283
284 (Contributed by Brett Cannon.)
Raymond Hettinger1f251a02009-04-04 10:47:35 +0000285
Georg Brandlc47408a2008-12-04 18:44:53 +0000286.. ======================================================================
Antoine Pitroub5564522009-03-28 19:45:26 +0000287
288
289Optimizations
Raymond Hettinger79d0b0e2009-04-07 07:11:00 +0000290=============
Antoine Pitroub5564522009-03-28 19:45:26 +0000291
292Major performance enhancements have been added:
293
294* The new I/O library (as defined in :pep:`3116`) was mostly written in
295 Python and quickly proved to be a problematic bottleneck in Python 3.0.
296 In Python 3.1, the I/O library has been entirely rewritten in C and is
297 2 to 20 times faster depending on the task at hand. The pure Python
298 version is still available for experimentation purposes through
299 the ``_pyio`` module.
300
301 (Contributed by Amaury Forgeot d'Arc and Antoine Pitrou.)
302
Raymond Hettinger8daab402009-04-04 13:01:19 +0000303* Added a heuristic so that tuples and dicts containing only untrackable objects
Raymond Hettingere7ec57d2009-04-04 11:08:48 +0000304 are not tracked by the garbage collector. This can reduce the size of
305 collections and therefore the garbage collection overhead on long-running
306 programs, depending on their particular use of datatypes.
307
308 (Contributed by Antoine Pitrou, :issue:`4688`.)
309
Raymond Hettingerf41857e2009-04-04 11:59:00 +0000310* Enabling a configure option named ``--with-computed-gotos``
311 on compilers that support it (notably: gcc, SunPro, icc), the bytecode
312 evaluation loop is compiled with a new dispatch mechanism which gives
Raymond Hettinger2a027772009-04-04 12:46:57 +0000313 speedups of up to 20%, depending on the system, the compiler, and
314 the benchmark.
Raymond Hettingerf41857e2009-04-04 11:59:00 +0000315
Raymond Hettinger2a027772009-04-04 12:46:57 +0000316 (Contributed by Antoine Pitrou along with a number of other participants,
317 :issue:`4753`).
Raymond Hettingerf41857e2009-04-04 11:59:00 +0000318
319* The decoding of UTF-8, UTF-16 and LATIN-1 is now two to four times
320 faster.
321
322 (Contributed by Antoine Pitrou and Amaury Forgeot d'Arc, :issue:`4868`.)
323
Raymond Hettingerc4f6d292009-04-04 12:35:58 +0000324* The :mod:`json` module is getting a C extension to substantially improve
325 its performance. The code is expected to be added in-time for the beta
326 release.
327
328 (Contributed by Bob Ippolito.)
329
330* Integers are now stored internally either in base 2**15 or in base
331 2**30, the base being determined at build time. Previously, they
332 were always stored in base 2**15. Using base 2**30 gives
333 significant performance improvements on 64-bit machines, but
334 benchmark results on 32-bit machines have been mixed. Therefore,
335 the default is to use base 2**30 on 64-bit machines and base 2**15
336 on 32-bit machines; on Unix, there's a new configure option
337 ``--enable-big-digits`` that can be used to override this default.
338
339 Apart from the performance improvements this change should be invisible to
340 end users, with one exception: for testing and debugging purposes there's a
Raymond Hettinger79d0b0e2009-04-07 07:11:00 +0000341 new :attr:`sys.int_info` that provides information about the
Raymond Hettingerc4f6d292009-04-04 12:35:58 +0000342 internal format, giving the number of bits per digit and the size in bytes
343 of the C type used to store each digit::
344
345 >>> import sys
346 >>> sys.int_info
347 sys.int_info(bits_per_digit=30, sizeof_digit=4)
348
349 (Contributed by Mark Dickinson; :issue:`4258`.)
Antoine Pitroub5564522009-03-28 19:45:26 +0000350
351.. ======================================================================