blob: d54196ed28dc411f4ea3d9d4155dcd0e3369d565 [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
Raymond Hettinger1f251a02009-04-04 10:47:35 +00005:Author: Raymond Hettinger
Benjamin Peterson34328292008-12-05 03:05:29 +00006:Release: |release|
7:Date: |today|
Georg Brandlc47408a2008-12-04 18:44:53 +00008
9.. $Id$
10 Rules for maintenance:
Georg Brandl48310cd2009-01-03 21:18:54 +000011
Georg Brandlc47408a2008-12-04 18:44:53 +000012 * 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 Brandl48310cd2009-01-03 21:18:54 +000015
Georg Brandlc47408a2008-12-04 18:44:53 +000016 * The maintainer will go through Misc/NEWS periodically and add
17 changes; it's therefore more important to add your changes to
Raymond Hettingerb9b33192009-04-08 09:38:32 +000018 Misc/NEWS than to this file.
Georg Brandl48310cd2009-01-03 21:18:54 +000019
Georg Brandlc47408a2008-12-04 18:44:53 +000020 * 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 Brandl48310cd2009-01-03 21:18:54 +000025
Georg Brandlc47408a2008-12-04 18:44:53 +000026 * 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 Brandl48310cd2009-01-03 21:18:54 +000029
Georg Brandlc47408a2008-12-04 18:44:53 +000030 * 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 Brandl48310cd2009-01-03 21:18:54 +000034
Georg Brandlc47408a2008-12-04 18:44:53 +000035 * 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 Brandl48310cd2009-01-03 21:18:54 +000037
Georg Brandlc47408a2008-12-04 18:44:53 +000038 * Credit the author of a patch or bugfix. Just the name is
Raymond Hettingerb9b33192009-04-08 09:38:32 +000039 sufficient; the e-mail address isn't necessary.
Georg Brandl48310cd2009-01-03 21:18:54 +000040
Georg Brandlc47408a2008-12-04 18:44:53 +000041 * It's helpful to add the bug/patch number as a comment:
Georg Brandl48310cd2009-01-03 21:18:54 +000042
Georg Brandlc47408a2008-12-04 18:44:53 +000043 % Patch 12345
44 XXX Describe the transmogrify() function added to the socket
45 module.
46 (Contributed by P.Y. Developer.)
Georg Brandl48310cd2009-01-03 21:18:54 +000047
Georg Brandlc47408a2008-12-04 18:44:53 +000048 This saves the maintainer the effort of going through the SVN log
Raymond Hettingerb9b33192009-04-08 09:38:32 +000049 when researching a change.
Georg Brandlc47408a2008-12-04 18:44:53 +000050
51This article explains the new features in Python 3.1, compared to 3.0.
52
Georg Brandlc47408a2008-12-04 18:44:53 +000053
Raymond Hettinger1f251a02009-04-04 10:47:35 +000054PEP 372: Ordered Dictionaries
55=============================
56
57Regular Python dictionaries iterate over key/value pairs in arbitrary order.
58Over the years, a number of authors have written alternative implementations
59that remember the order that the keys were originally inserted. Based on
60the experiences from those implementations, the :mod:`collections` module
61now has an :class:`OrderedDict` class.
62
63The OrderedDict API is substantially the same as regular dictionaries
64but will iterate over keys and values in a guaranteed order depending on
65when a key was first inserted. If a new entry overwrites an existing entry,
66the original insertion position is left unchanged. Deleting an entry and
67reinserting it will move it to the end.
68
69The standard library now supports use of ordered dictionaries in several
Raymond Hettingerc4f6d292009-04-04 12:35:58 +000070modules. The :mod:`ConfigParser` module uses them by default. This lets
Raymond Hettinger1f251a02009-04-04 10:47:35 +000071configuration files be read, modified, and then written back in their original
72order. The :mod:`collections` module's :meth:`namedtuple._asdict` method now
Raymond Hettingerc4f6d292009-04-04 12:35:58 +000073returns an ordered dictionary with the values appearing in the same order as
74the underlying tuple indicies. The :mod:`json` module is being built-out with
75an *object_pairs_hook* to allow OrderedDicts to be built by the decoder.
Raymond Hettinger347396a2009-04-07 23:10:59 +000076Support was also added for third-party tools like `PyYAML <http://pyyaml.org/>`_.
Raymond Hettinger1f251a02009-04-04 10:47:35 +000077
78.. seealso::
79
80 :pep:`372` - Ordered Dictionaries
Raymond Hettingerf84dfe52009-04-04 13:13:56 +000081 PEP written by Armin Ronacher and Raymond Hettinger. Implementation
82 written by Raymond Hettinger.
Raymond Hettinger1f251a02009-04-04 10:47:35 +000083
Raymond Hettingerb9b33192009-04-08 09:38:32 +000084
Raymond Hettinger1f251a02009-04-04 10:47:35 +000085PEP 378: Format Specifier for Thousands Separator
86=================================================
87
88The builtin :func:`format` function and the :meth:`str.format` method use
89a mini-language that now includes a simple, non-locale aware way to format
90a number with a thousands separator. That provides a way to humanize a
91program's output, improving its professional appearance and readability::
92
93 >>> format(Decimal('1234567.89'), ',f')
94 '1,234,567.89'
95
Raymond Hettingerc4f6d292009-04-04 12:35:58 +000096The currently supported types are :class:`int` and :class:`decimal.Decimal`.
Raymond Hettinger1f251a02009-04-04 10:47:35 +000097Support for :class:`float` is expected before the beta release.
98Discussions are underway about how to specify alternative separators
Raymond Hettingerc4f6d292009-04-04 12:35:58 +000099like dots, spaces, apostrophes, or underscores. Locale-aware applications
100should use the existing *n* format specifier which already has some support
101for thousands separators.
Raymond Hettinger1f251a02009-04-04 10:47:35 +0000102
103.. seealso::
104
105 :pep:`378` - Format Specifier for Thousands Separator
106 PEP written by Raymond Hettinger; implemented by Eric Smith and
107 Mark Dickinson.
108
109
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000110Other Language Changes
111======================
112
113Some smaller changes made to the core Python language are:
114
115* The :func:`int` type gained a ``bit_length`` method that returns the
116 number of bits necessary to represent its argument in binary::
117
118 >>> n = 37
119 >>> bin(37)
120 '0b100101'
121 >>> n.bit_length()
122 6
123 >>> n = 2**123-1
124 >>> n.bit_length()
125 123
126 >>> (n+1).bit_length()
127 124
128
Raymond Hettingerc4f6d292009-04-04 12:35:58 +0000129 (Contributed by Fredrik Johansson, Victor Stinner, Raymond Hettinger,
130 and Mark Dickinson; :issue:`3439`.)
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000131
Raymond Hettinger79d0b0e2009-04-07 07:11:00 +0000132* The fields in :func:`format` strings can now be automatically
133 numbered::
134
135 >>> 'Sir {} of {}'.format('Gallahad', 'Camelot')
136 'Sir Gallahad of Camelot'
137
138 Formerly, the string would have required numbered fields such as:
139 ``'Sir {0} of {1}'``.
140
141 (Contributed by Eric Smith; :issue:`5237`.)
142
143* ``round(x, n)`` now returns an integer if *x* is an integer.
144 Previously it returned a float::
145
146 >>> round(1123, -2)
147 1100
148
149 (Contributed by Mark Dickinson; :issue:`4707`.)
150
Raymond Hettinger79d0b0e2009-04-07 07:11:00 +0000151
152New, Improved, and Deprecated Modules
153=====================================
154
Raymond Hettinger1f251a02009-04-04 10:47:35 +0000155* Added a :class:`collections.Counter` class to support convenient
156 counting of unique items in a sequence or iterable::
157
158 >>> Counter(['red', 'blue', 'red', 'green', 'blue', 'blue'])
159 Counter({'blue': 3, 'red': 2, 'green': 1})
160
161 (Contributed by Raymond Hettinger; :issue:`1696199`.)
162
Raymond Hettingerc1bd49a2009-04-06 23:11:08 +0000163* Added a new module, :mod:`tkinter.ttk` for access to the Tk themed widget set.
Raymond Hettingerdbd51b52009-04-06 22:45:52 +0000164 The basic idea of ttk is to separate, to the extent possible, the code
Raymond Hettingerf84dfe52009-04-04 13:13:56 +0000165 implementing a widget's behavior from the code implementing its appearance.
166
Raymond Hettingerd1b3de32009-04-08 00:09:26 +0000167 (Contributed by Guilherme Polo; :issue:`2983`.)
Raymond Hettingerf84dfe52009-04-04 13:13:56 +0000168
Raymond Hettingerf4cc2c42009-04-06 22:39:03 +0000169* The :class:`gzip.GzipFile` and :class:`bz2.BZ2File` classes now support
170 the context manager protocol::
Raymond Hettinger1f251a02009-04-04 10:47:35 +0000171
Raymond Hettingerf4cc2c42009-04-06 22:39:03 +0000172 >>> # Automatically close file after writing
173 >>> with gzip.GzipFile(filename, "wb") as f:
174 ... f.write(b"xxx")
175
176 (Contributed by Antoine Pitrou.)
Raymond Hettinger1f251a02009-04-04 10:47:35 +0000177
Raymond Hettinger76efa2b2009-04-06 23:11:47 +0000178* The :mod:`decimal.Decimal` module now supports methods for creating a
179 decimal object from a binary :class:`float`. The conversion is
Raymond Hettinger1f251a02009-04-04 10:47:35 +0000180 exact but can sometimes be surprising::
181
182 >>> Decimal.from_float(1.1)
183 Decimal('1.100000000000000088817841970012523233890533447265625')
184
185 The long decimal result shows the actual binary fraction being
186 stored for *1.1*. The fraction has many digits because *1.1* cannot
187 be exactly represented in binary.
188
189 (Contributed by Raymond Hettinger and Mark Dickinson.)
190
Raymond Hettingere7ec57d2009-04-04 11:08:48 +0000191* The :mod:`itertools` module grew two new functions. The
192 :func:`itertools.combinations_with_replacement` function is one of
193 four for generating combinatorics including permutations and Cartesian
194 products. The :func:`itertools.compress` function mimics its namesake
195 from APL. Also, the existing :func:`itertools.count` function now has
196 an optional *step* argument and can accept any type of counting
197 sequence including :class:`fractions.Fraction` and
Raymond Hettinger8d97ccb2009-04-06 17:55:05 +0000198 :class:`decimal.Decimal`::
199
200 >>> [p+q for p,q in combinations_with_replacement('LOVE', 2)]
201 ['LL', 'LO', 'LV', 'LE', 'OO', 'OV', 'OE', 'VV', 'VE', 'EE']
202
203 >>> list(compress(data=range(10), selectors=[0,0,1,1,0,1,0,1,0,0]))
204 [2, 3, 5, 7]
205
206 >>> c = count(start=Fraction(1,2), step=Fraction(1,6))
207 >>> next(c), next(c), next(c), next(c)
208 (Fraction(1, 2), Fraction(2, 3), Fraction(5, 6), Fraction(1, 1))
Raymond Hettingere7ec57d2009-04-04 11:08:48 +0000209
210 (Contributed by Raymond Hettinger.)
211
Raymond Hettingerf41857e2009-04-04 11:59:00 +0000212* :func:`collections.namedtuple` now supports a keyword argument
213 *rename* which lets invalid fieldnames be automatically converted to
214 positional names in the form _0, _1, etc. This is useful when
215 the field names are being created by an external source such as a
Raymond Hettinger4c0b1e42009-04-08 07:49:03 +0000216 CSV header, SQL field list, or user input::
217
218 >>> query = input('Enter a query: ')
219 Enter a query: SELECT region, dept, count(*) FROM main GROUPBY region, dept
220
221 >>> cursor.execute(query)
222 >>> query_fields = [desc[0] for desc in cursor.description]
223 >>> UserQuery = namedtuple('UserQuery', query_fields, rename=True)
224 >>> pprint.pprint([UserQuery(*row) for row in cursor])
225 [UserQuery(region='South', dept='Shipping', _2=185),
226 UserQuery(region='North', dept='Accounting', _2=37),
227 UserQuery(region='West', dept='Sales', _2=419)]
Raymond Hettingerf41857e2009-04-04 11:59:00 +0000228
229 (Contributed by Raymond Hettinger; :issue:`1818`.)
230
Raymond Hettingerf41857e2009-04-04 11:59:00 +0000231* The :func:`re.sub`, :func:`re.subn` and :func:`re.split` functions now
232 accept a flags parameter.
233
234 (Contributed by Gregory Smith.)
235
236* The :mod:`runpy` module which supports the ``-m`` command line switch
237 now supports the execution of packages by looking for and executing
238 a ``__main__`` submodule when a package name is supplied.
239
240 (Contributed by Andi Vajda; :issue:`4195`.)
241
242* The :mod:`pdb` module can now access and display source code loaded via
243 :mod:`zipimport` (or any other conformant :pep:`302` loader).
244
245 (Contributed by Alexander Belopolsky; :issue:`4201`.)
246
247* :class:`functools.partial` objects can now be pickled.
248
249 (Suggested by Antoine Pitrou and Jesse Noller. Implemented by
250 Jack Diedrich; :issue:`5228`.)
251
Raymond Hettinger8e330512009-04-04 13:20:55 +0000252* Add :mod:`pydoc` help topics for symbols so that ``help('@')``
253 works as expected in the interactive environment.
254
255 (Contributed by David Laban; :issue:`4739`.)
256
Raymond Hettingerc4f6d292009-04-04 12:35:58 +0000257* The :mod:`unittest` module now supports skipping individual tests or classes
258 of tests. And it supports marking a test as a expected failure, a test that
Raymond Hettinger8daab402009-04-04 13:01:19 +0000259 is known to be broken, but shouldn't be counted as a failure on a
Raymond Hettinger8d97ccb2009-04-06 17:55:05 +0000260 TestResult::
261
262 class TestGizmo(unittest.TestCase):
263
264 @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
265 def test_gizmo_on_windows(self):
266 ...
267
268 @unittest.expectedFailure
269 def test_gimzo_without_required_library(self):
270 ...
Raymond Hettingerf41857e2009-04-04 11:59:00 +0000271
Raymond Hettingerc4f6d292009-04-04 12:35:58 +0000272 (Contributed by Benjamin Peterson.)
273
274* A new module, :mod:`importlib` was added. It provides a complete, portable,
275 pure Python reference implementation of the *import* statement and its
Benjamin Peterson3fa0fb42009-04-04 12:42:53 +0000276 counterpart, the :func:`__import__` function. It represents a substantial
Raymond Hettingerc4f6d292009-04-04 12:35:58 +0000277 step forward in documenting and defining the actions that take place during
278 imports.
279
280 (Contributed by Brett Cannon.)
Raymond Hettinger1f251a02009-04-04 10:47:35 +0000281
Antoine Pitroub5564522009-03-28 19:45:26 +0000282
283Optimizations
Raymond Hettinger79d0b0e2009-04-07 07:11:00 +0000284=============
Antoine Pitroub5564522009-03-28 19:45:26 +0000285
286Major performance enhancements have been added:
287
288* The new I/O library (as defined in :pep:`3116`) was mostly written in
289 Python and quickly proved to be a problematic bottleneck in Python 3.0.
290 In Python 3.1, the I/O library has been entirely rewritten in C and is
291 2 to 20 times faster depending on the task at hand. The pure Python
292 version is still available for experimentation purposes through
293 the ``_pyio`` module.
294
295 (Contributed by Amaury Forgeot d'Arc and Antoine Pitrou.)
296
Raymond Hettinger8daab402009-04-04 13:01:19 +0000297* Added a heuristic so that tuples and dicts containing only untrackable objects
Raymond Hettingere7ec57d2009-04-04 11:08:48 +0000298 are not tracked by the garbage collector. This can reduce the size of
299 collections and therefore the garbage collection overhead on long-running
300 programs, depending on their particular use of datatypes.
301
302 (Contributed by Antoine Pitrou, :issue:`4688`.)
303
Raymond Hettingerf41857e2009-04-04 11:59:00 +0000304* Enabling a configure option named ``--with-computed-gotos``
305 on compilers that support it (notably: gcc, SunPro, icc), the bytecode
306 evaluation loop is compiled with a new dispatch mechanism which gives
Raymond Hettinger2a027772009-04-04 12:46:57 +0000307 speedups of up to 20%, depending on the system, the compiler, and
308 the benchmark.
Raymond Hettingerf41857e2009-04-04 11:59:00 +0000309
Raymond Hettinger2a027772009-04-04 12:46:57 +0000310 (Contributed by Antoine Pitrou along with a number of other participants,
311 :issue:`4753`).
Raymond Hettingerf41857e2009-04-04 11:59:00 +0000312
313* The decoding of UTF-8, UTF-16 and LATIN-1 is now two to four times
314 faster.
315
316 (Contributed by Antoine Pitrou and Amaury Forgeot d'Arc, :issue:`4868`.)
317
Raymond Hettingerc4f6d292009-04-04 12:35:58 +0000318* The :mod:`json` module is getting a C extension to substantially improve
319 its performance. The code is expected to be added in-time for the beta
320 release.
321
322 (Contributed by Bob Ippolito.)
323
324* Integers are now stored internally either in base 2**15 or in base
325 2**30, the base being determined at build time. Previously, they
326 were always stored in base 2**15. Using base 2**30 gives
327 significant performance improvements on 64-bit machines, but
328 benchmark results on 32-bit machines have been mixed. Therefore,
329 the default is to use base 2**30 on 64-bit machines and base 2**15
330 on 32-bit machines; on Unix, there's a new configure option
331 ``--enable-big-digits`` that can be used to override this default.
332
333 Apart from the performance improvements this change should be invisible to
334 end users, with one exception: for testing and debugging purposes there's a
Raymond Hettinger79d0b0e2009-04-07 07:11:00 +0000335 new :attr:`sys.int_info` that provides information about the
Raymond Hettingerc4f6d292009-04-04 12:35:58 +0000336 internal format, giving the number of bits per digit and the size in bytes
337 of the C type used to store each digit::
338
339 >>> import sys
340 >>> sys.int_info
341 sys.int_info(bits_per_digit=30, sizeof_digit=4)
342
343 (Contributed by Mark Dickinson; :issue:`4258`.)
Antoine Pitroub5564522009-03-28 19:45:26 +0000344