blob: 895e289c828b8d425048b8c0804f048d6ad431e1 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001****************************
Guido van Rossumeb3d8d42008-12-02 00:56:25 +00002 What's New In Python 3.0
Georg Brandl116aa622007-08-15 14:28:22 +00003****************************
4
Guido van Rossum715287f2008-12-02 22:34:15 +00005.. XXX Add trademark info for Apple, Microsoft.
Georg Brandl116aa622007-08-15 14:28:22 +00006
Guido van Rossum715287f2008-12-02 22:34:15 +00007.. XXX Remove duplicates; just put info in the most relevant section.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +00008
Guido van Rossum4a98a2a2008-11-21 18:35:43 +00009:Author: Guido van Rossum
10:Release: |release|
11:Date: |today|
12
13.. $Id$
14 Rules for maintenance:
Georg Brandl5a165582007-08-31 06:15:01 +000015
16 * Anyone can add text to this document. Do not spend very much time
17 on the wording of your changes, because your text will probably
18 get rewritten to some degree.
19
20 * The maintainer will go through Misc/NEWS periodically and add
21 changes; it's therefore more important to add your changes to
22 Misc/NEWS than to this file.
23
24 * This is not a complete list of every single change; completeness
25 is the purpose of Misc/NEWS. Some changes I consider too small
26 or esoteric to include. If such a change is added to the text,
27 I'll just remove it. (This is another reason you shouldn't spend
28 too much time on writing your addition.)
29
30 * If you want to draw your new text to the attention of the
31 maintainer, add 'XXX' to the beginning of the paragraph or
32 section.
33
34 * It's OK to just add a fragmentary note about a change. For
35 example: "XXX Describe the transmogrify() function added to the
36 socket module." The maintainer will research the change and
37 write the necessary text.
38
39 * You can comment out your additions if you like, but it's not
40 necessary (especially when a final release is some months away).
41
42 * Credit the author of a patch or bugfix. Just the name is
43 sufficient; the e-mail address isn't necessary.
44
45 * It's helpful to add the bug/patch number as a comment:
46
47 % Patch 12345
48 XXX Describe the transmogrify() function added to the socket
49 module.
50 (Contributed by P.Y. Developer.)
51
52 This saves the maintainer the effort of going through the SVN log
53 when researching a change.
Georg Brandl116aa622007-08-15 14:28:22 +000054
Guido van Rossumeb3d8d42008-12-02 00:56:25 +000055This article explains the new features in Python 3.0, compared to 2.6.
Guido van Rossumc46ee542008-12-02 23:46:46 +000056Python 3.0, also known as "Python 3000" or "Py3K", is the first ever
Guido van Rossum715287f2008-12-02 22:34:15 +000057*intentionally incompatible* release. There are more changes than in
58a typical release, and more that are important for all Python users.
59Nevertheless, after digesting the changes, you'll find that Python
60really hasn't changed all that much -- by and large, we're merely
61fixing well-known annoyances and warts.
Guido van Rossumb197f3c2007-08-31 00:37:00 +000062
63This article doesn't attempt to provide a complete specification of
64the new features, but instead provides a convenient overview. For
65full details, you should refer to the documentation for Python 3.0. If
66you want to understand the complete implementation and design
67rationale, refer to the PEP for a particular new feature.
Georg Brandl116aa622007-08-15 14:28:22 +000068
Georg Brandl5a165582007-08-31 06:15:01 +000069.. Compare with previous release in 2 - 3 sentences here.
70.. add hyperlink when the documentation becomes available online.
Georg Brandl116aa622007-08-15 14:28:22 +000071
Georg Brandl5a165582007-08-31 06:15:01 +000072.. ======================================================================
73.. Large, PEP-level features and changes should be described here.
74.. Should there be a new section here for 3k migration?
75.. Or perhaps a more general section describing module changes/deprecation?
76.. sets module deprecated
77.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +000078
79
Guido van Rossumb197f3c2007-08-31 00:37:00 +000080Common Stumbling Blocks
81=======================
82
Guido van Rossum715287f2008-12-02 22:34:15 +000083This section lists those few changes that are most likely to trip you
84up if you're used to Python 2.5.
Guido van Rossumb197f3c2007-08-31 00:37:00 +000085
Guido van Rossumeb3d8d42008-12-02 00:56:25 +000086Print Is A Function
87-------------------
Guido van Rossumdff1c312007-09-06 14:46:41 +000088
Guido van Rossum715287f2008-12-02 22:34:15 +000089The :keyword:`print` statement has been replaced with a :func:`print`
90function, with keyword arguments to replace most of the special syntax
91of the old :keyword:`print` statement (:pep:`3105`). Examples::
Guido van Rossumdff1c312007-09-06 14:46:41 +000092
Guido van Rossumeb3d8d42008-12-02 00:56:25 +000093 Old: print "The answer is", 2*2
94 New: print("The answer is", 2*2)
Guido van Rossumdff1c312007-09-06 14:46:41 +000095
Guido van Rossumeb3d8d42008-12-02 00:56:25 +000096 Old: print x, # Trailing comma suppresses newline
97 New: print(x, end=" ") # Appends a space instead of a newline
Guido van Rossumdff1c312007-09-06 14:46:41 +000098
Guido van Rossumeb3d8d42008-12-02 00:56:25 +000099 Old: print # Prints a newline
100 New: print() # You must call the function!
Guido van Rossumdff1c312007-09-06 14:46:41 +0000101
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000102 Old: print >>sys.stderr, "fatal error"
103 New: print("fatal error", file=sys.stderr)
Guido van Rossumdff1c312007-09-06 14:46:41 +0000104
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000105 Old: print (x, y) # prints repr((x, y))
106 New: print((x, y)) # Not the same as print(x, y)!
Guido van Rossumdff1c312007-09-06 14:46:41 +0000107
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000108You can also customize the separator between items, e.g.::
Guido van Rossumdff1c312007-09-06 14:46:41 +0000109
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000110 print("There are <", 2**32, "> possibilities!", sep="")
Guido van Rossumdff1c312007-09-06 14:46:41 +0000111
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000112which produces::
Guido van Rossumdff1c312007-09-06 14:46:41 +0000113
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000114 There are <4294967296> possibilities!
Guido van Rossumdff1c312007-09-06 14:46:41 +0000115
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000116Note:
Guido van Rossumdff1c312007-09-06 14:46:41 +0000117
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000118* The :func:`print` function doesn't support the "softspace" feature of
Guido van Rossum715287f2008-12-02 22:34:15 +0000119 the old :keyword:`print` statement. For example, in Python 2.x,
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000120 ``print "A\n", "B"`` would write ``"A\nB\n"``; but in Python 3.0,
121 ``print("A\n", "B")`` writes ``"A\n B\n"``.
Guido van Rossumdff1c312007-09-06 14:46:41 +0000122
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000123* Initially, you'll be finding yourself typing the old ``print x``
124 a lot in interactive mode. Time to retrain your fingers to type
125 ``print(x)`` instead!
Guido van Rossumdff1c312007-09-06 14:46:41 +0000126
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000127* When using the ``2to3`` source-to-source conversion tool, all
Guido van Rossum715287f2008-12-02 22:34:15 +0000128 :keyword:`print` statements are automatically converted to
129 :func:`print` function calls, so this is mostly a non-issue for
130 larger projects.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000131
Guido van Rossum38287682008-12-03 02:03:19 +0000132Text Vs. Data Instead Of Unicode Vs. 8-bit
133------------------------------------------
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000134
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000135Everything you thought you knew about binary data and Unicode has
Guido van Rossum38287682008-12-03 02:03:19 +0000136changed:
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000137
Guido van Rossum38287682008-12-03 02:03:19 +0000138XXX HIRO
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000139
Guido van Rossum38287682008-12-03 02:03:19 +0000140* Python 3.0 uses the concepts of *text* and (binary) *data* instead
141 of Unicode strings and 8-bit strings. All text is Unicode; however
142 *encoded* Unicode is represented as binary data. The type used to
143 hold text is :class:`str`, the type used to hold data is
144 :class:`bytes`. The difference is that any attempt to mix text and
145 data in Python 3.0 raises a TypeError exception, whereas if you were
146 to mix Unicode and 8-bit strings in Python 2.x, you would only get
147 an exception if the 8-bit string contained non-ASCII values. As a
148 consequence, pretty much all code that uses Unicode, encodings or
149 binary data most likely has to change. The change is for the
150 better, as in the 2.x world there were numerous bugs having to do
151 with mixing encoded and unencoded text.
152
153* You no longer use ``u"..."`` literals for Unicode text. However,
154 you must use ``b"..."`` literals for binary data.
Guido van Rossum73961352008-12-03 00:54:52 +0000155
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000156* Files opened as text files (still the default mode for :func:`open`)
157 always use an encoding to map between strings (in memory) and bytes
158 (on disk). Binary files (opened with a ``b`` in the mode argument)
159 always use bytes in memory. This means that if a file is opened
160 using an incorrect mode or encoding, I/O will likely fail. There is
161 a platform-dependent default encoding, which on Unixy platforms can
162 be set with the ``LANG`` environment variable (and sometimes also
163 with some other platform-specific locale-related environment
164 variables). In many cases, but not all, the system default is
Walter Dörwalda4438562008-12-02 11:55:30 +0000165 UTF-8; you should never count on this default. Any application
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000166 reading or writing more than pure ASCII text should probably have a
167 way to override the encoding.
168
Guido van Rossum715287f2008-12-02 22:34:15 +0000169* The builtin :class:`basestring` abstract type was removed. Use
170 :class:`str` instead. The :class:`str` and :class:`bytes` types
171 don't have functionality enough in common to warrant a shared base
172 class.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000173
Guido van Rossum38287682008-12-03 02:03:19 +0000174* All backslashes in raw strings are interpreted literally. This
175 means that ``'\U'`` and ``'\u'`` escapes in raw strings are not
176 treated specially.
177
178XXX Deal with dupes below
179
180* There is only one text string type; its name is :class:`str` but its
181 behavior and implementation are like :class:`unicode` in 2.x.
182
183* The :class:`basestring` superclass has been removed. The ``2to3``
184 tool (see below) replaces every occurrence of :class:`basestring`
185 with :class:`str`.
186
187* :pep:`3137`: There is a new type, :class:`bytes`, to represent
188 binary data (and encoded text, which is treated as binary data until
189 it is decoded). The :class:`str` and :class:`bytes` types cannot be
190 mixed; you must always explicitly convert between them, using the
191 :meth:`str.encode` (str -> bytes) or :meth:`bytes.decode` (bytes ->
192 str) methods.
193
194* Like :class:`str`, the :class:`bytes` type is immutable. There is a
195 separate *mutable* type to hold buffered binary data,
196 :class:`bytearray`. Nearly all APIs that accept :class:`bytes` also
197 accept :class:`bytearray`. The mutable API is based on
198 :class:`collections.MutableSequence`.
199
200* :pep:`3138`: The :func:`repr` of a string no longer escapes
201 non-ASCII characters. It still escapes control characters and code
202 points with non-printable status in the Unicode standard, however.
203
204* :pep:`3120`: The default source encoding is now UTF-8.
205
206* :pep:`3131`: Non-ASCII letters are now allowed in identifiers.
207 (However, the standard library remains ASCII-only with the exception
208 of contributor names in comments.)
209
210* :pep:`3116`: New I/O implementation. The API is nearly 100%
211 backwards compatible, but completely reimplemented (currently largely
212 in Python). Also, binary files use bytes instead of strings.
213
214* The :mod:`StringIO` and :mod:`cStringIO` modules are gone. Instead,
215 import :class:`io.StringIO` or :class:`io.BytesIO`, for text and
216 data respectively.
217
Guido van Rossum715287f2008-12-02 22:34:15 +0000218* See also the :ref:`unicode-howto`, which was updated for Python 3.0.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000219
Gregory P. Smithf3655c42008-12-02 23:52:53 +0000220Views And Iterators Instead Of Lists
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000221-------------------------------------
222
223Some well-known APIs no longer return lists:
224
225* :class:`dict` methods :meth:`dict.keys`, :meth:`dict.items` and
226 :meth:`dict.values` return "views" instead of lists. For example,
227 this no longer works: ``k = d.keys(); k.sort()``. Use ``k =
Guido van Rossum73961352008-12-03 00:54:52 +0000228 sorted(d)`` instead (this works in Python 2.5 too, and is just
229 as efficient).
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000230
231* Also, the :meth:`dict.iterkeys`, :meth:`dict.iteritems` and
232 :meth:`dict.itervalues` methods are no longer supported.
Guido van Rossum4a98a2a2008-11-21 18:35:43 +0000233
Georg Brandlec17d202008-02-02 10:44:37 +0000234* :func:`map` and :func:`filter` return iterators. A quick fix is e.g.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000235 ``list(map(...))``, but a better fix is often to use a list
Georg Brandlec17d202008-02-02 10:44:37 +0000236 comprehension (especially when the original code uses :keyword:`lambda`).
237 Particularly tricky is :func:`map` invoked for the side effects of the
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000238 function; the correct transformation is to use a for-loop.
239
Guido van Rossum73961352008-12-03 00:54:52 +0000240* :func:`range` now behaves like :func:`xrange` used to behave, except
241 it works with values of arbitrary size. The latter no longer
242 exists.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000243
244* :func:`zip` now returns an iterator.
245
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000246Ordering Comparisons
247--------------------
248
249Python 3.0 has simplified the rules for ordering comparisons:
250
251* The ordering comparison operators (``<``, ``<=``, ``>=``, ``>``)
252 raise a TypeError exception when the operands don't have a
253 meaningful natural ordering. Thus, expressions like ``1 < ''``, ``0
254 > None`` or ``len <= len`` are no longer valid. A corollary is that
255 sorting a heterogeneous list no longer makes sense -- all the
256 elements must be comparable to each other. Note that this does not
257 apply to the ``==`` and ``!=`` operators: objects of different
258 uncomparable types always compare unequal to each other, and an
259 object always compares equal to itself (i.e., ``x is y`` implies ``x
260 = y``; this is true even for ``NaN``).
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000261
Kurt B. Kaiser2bc6b5e2008-02-13 18:03:11 +0000262* :meth:`builtin.sorted` and :meth:`list.sort` no longer accept the *cmp*
Kurt B. Kaiser9d0d6162008-02-14 02:47:50 +0000263 argument providing a comparison function. Use the *key* argument
Kurt B. Kaiser2bc6b5e2008-02-13 18:03:11 +0000264 instead. N.B. the *key* and *reverse* arguments are now "keyword-only".
Kurt B. Kaisera1401012008-02-13 16:09:27 +0000265
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000266* The :func:`cmp` function is gone, and the :meth:`__cmp__` special
267 method is no longer supported. Use :meth:`__lt__` for sorting,
268 :meth:`__eq__` with :meth:`__hash__`, and other rich comparisons as
Guido van Rossum73961352008-12-03 00:54:52 +0000269 needed. (If you really need the :func:`cmp` functionality, you
270 could use the expression ``(a > b) - (a < b)`` as the equivalent for
271 ``cmp(a, b)``.)
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000272
273Integers
274--------
275
Guido van Rossum73961352008-12-03 00:54:52 +0000276* :pep:`0237`: Essentially, :class:`long` renamed to :class:`int`.
277 That is, there is only one built-in integral type, named
278 :class:`int`; but it behaves mostly like the old :class:`long` type.
Guido van Rossum4a98a2a2008-11-21 18:35:43 +0000279
Guido van Rossum73961352008-12-03 00:54:52 +0000280* :pep:`0238`: An expression like ``1/2`` returns a float. Use
281 ``1//2`` to get the truncating behavior. (The latter syntax has
282 existed for years, at least since Python 2.2.)
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000283
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000284* The :data:`sys.maxint` constant was removed, since there is no
285 longer a limit to the value of ints. However, :data:`sys.maxsize`
286 can be used as an integer larger than any practical list or string
287 index. It conforms to the implementation's "natural" integer size
288 and is typically the same as :data:`sys.maxint` in previous releases
289 on the same platform (assuming the same build options).
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000290
Guido van Rossum73961352008-12-03 00:54:52 +0000291* The :func:`repr` of a long integer doesn't include the trailing ``L``
292 anymore, so code that unconditionally strips that character will
293 chop off the last digit instead. (Use :func:`str` instead.)
294
295* Octal literals are no longer of the form ``0720``; use ``0o720``
296 instead.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000297
298
Guido van Rossum73961352008-12-03 00:54:52 +0000299Overview Of Syntax Changes
300==========================
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000301
Guido van Rossum73961352008-12-03 00:54:52 +0000302This section gives a brief overview of every *syntactic* change in
303Python 3.0.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000304
Guido van Rossum38287682008-12-03 02:03:19 +0000305New Syntax
306----------
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000307
Guido van Rossum73961352008-12-03 00:54:52 +0000308* :pep:`3107`: Function argument and return value annotations. This
309 provides a standardized way of annotating a function's parameters
310 and return value. There are no semantics attached to such
311 annotations except that they can be introspected at runtime using
312 the :attr:`__annotations__` attribute. The intent is to encourage
313 experimentation through metaclasses, decorators or frameworks.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000314
Guido van Rossum715287f2008-12-02 22:34:15 +0000315* :pep:`3102`: Keyword-only arguments. Named parameters occurring
316 after ``*args`` in the parameter list *must* be specified using
317 keyword syntax in the call. You can also use a bare ``*`` in the
318 parameter list to indicate that you don't accept a variable-length
319 argument list, but you do have keyword-only arguments.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000320
321* Keyword arguments are allowed after the list of base classes in a
322 class definition. This is used by the new convention for specifying
Guido van Rossum73961352008-12-03 00:54:52 +0000323 a metaclass (see :pep:`3115`), but can be used for other purposes as
324 well, as long as the metaclass supports it.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000325
Guido van Rossum715287f2008-12-02 22:34:15 +0000326* :pep:`3104`: :keyword:`nonlocal` statement. Using ``nonlocal x``
327 you can now assign directly to a variable in an outer (but
328 non-global) scope. :keyword:`nonlocal` is a new reserved word.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000329
Guido van Rossum715287f2008-12-02 22:34:15 +0000330* :pep:`3132`: Extended Iterable Unpacking. You can now write things
331 like ``a, b, *rest = some_sequence``. And even ``*rest, a =
332 stuff``. The ``rest`` object is always a (possibly empty) list; the
333 right-hand side may be any iterable. Example::
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000334
Guido van Rossum715287f2008-12-02 22:34:15 +0000335 (a, *rest, b) = range(5)
336
337 This sets *a* to ``0``, *b* to ``4``, and \*rest to ``[1, 2, 3]``.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000338
339* Dictionary comprehensions: ``{k: v for k, v in stuff}`` means the
Guido van Rossum73961352008-12-03 00:54:52 +0000340 same thing as ``dict(stuff)`` but is more flexible. (This is
341 :pep:`0274` vindicated. :-)
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000342
343* Set literals, e.g. ``{1, 2}``. Note that ``{}`` is an empty
Guido van Rossum715287f2008-12-02 22:34:15 +0000344 dictionary; use ``set()`` for an empty set. Set comprehensions are
Guido van Rossum73961352008-12-03 00:54:52 +0000345 also supported; e.g., ``{x for x in stuff}`` means the same thing as
Guido van Rossum715287f2008-12-02 22:34:15 +0000346 ``set(stuff)`` but is more flexible.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000347
348* New octal literals, e.g. ``0o720`` (already in 2.6). The old octal
Walter Dörwaldeab34c92008-12-02 11:58:09 +0000349 literals (``0720``) are gone.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000350
351* New binary literals, e.g. ``0b1010`` (already in 2.6).
352
353* Bytes literals are introduced with a leading ``b`` or ``B``.
354
Guido van Rossum38287682008-12-03 02:03:19 +0000355Changed Syntax
356--------------
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000357
358* New :keyword:`raise` statement syntax: ``raise [expr [from expr]]``.
Guido van Rossum715287f2008-12-02 22:34:15 +0000359 Also note that string exceptions are no longer legal (:pep:`0352`).
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000360
Guido van Rossum715287f2008-12-02 22:34:15 +0000361* :keyword:`as` and :keyword:`with` are now reserved words. (Since
362 2.6, actually.)
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000363
Guido van Rossum715287f2008-12-02 22:34:15 +0000364* :keyword:`True`, :keyword:`False`, and :keyword:`None` are reserved
365 words. (2.6 partially enforced the restrictions on :keyword:`None`
366 already.)
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000367
Guido van Rossum715287f2008-12-02 22:34:15 +0000368* Change from :keyword:`except` *exc*, *var* to
369 :keyword:`except` *exc* :keyword:`as` *var*. See :pep:`3110`.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000370
Guido van Rossum715287f2008-12-02 22:34:15 +0000371* List comprehensions no longer support the syntactic form
372 ``[... for var in item1, item2, ...]``. Use
373 ``[... for var in (item1, item2, ...)]`` instead.
374 Also note that list comprehensions have different semantics: they
375 are closer to syntactic sugar for a generator expression inside a
376 :func:`list` constructor, and in particular the loop control
377 variables are no longer leaked into the surrounding scope.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000378
Guido van Rossum715287f2008-12-02 22:34:15 +0000379* The *ellipsis* (``...``) can be used as an atomic expression
380 anywhere. (Previously it was only allowed in slices.) Also, it
381 *must* now be spelled as ``...``. (Previously it could also be
382 spelled as ``. . .``, by a mere accident of the grammar.)
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000383
Guido van Rossum38287682008-12-03 02:03:19 +0000384Removed Syntax
385--------------
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000386
Guido van Rossum715287f2008-12-02 22:34:15 +0000387* :pep:`3113`: Tuple parameter unpacking removed. You can no longer
388 write ``def foo(a, (b, c)): ...``.
389 Use ``def foo(a, b_c): b, c = b_c`` instead.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000390
Guido van Rossum715287f2008-12-02 22:34:15 +0000391* Removed backticks (use :func:`repr` instead).
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000392
Guido van Rossum715287f2008-12-02 22:34:15 +0000393* Removed ``<>`` (use ``!=`` instead).
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000394
395* Removed keyword: :func:`exec` is no longer a keyword; it remains as
396 a function. (Fortunately the function syntax was also accepted in
Guido van Rossum715287f2008-12-02 22:34:15 +0000397 2.x.) Also note that :func:`exec` no longer takes a stream argument;
398 instead of ``exec(f)`` you can use ``exec(f.read())``.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000399
400* Integer literals no longer support a trailing ``l`` or ``L``.
401
402* String literals no longer support a leading ``u`` or ``U``.
403
Guido van Rossum715287f2008-12-02 22:34:15 +0000404* The :keyword:`from` *module* :keyword:`import` ``*`` syntax is only
405 allowed at the module level, no longer inside functions.
406
407* The only acceptable syntax for relative imports is :keyword:`from`
408 ``.``[*module*] :keyword:`import` *name*; :keyword:`import` forms
409 not starting with ``.`` are always interpreted as absolute imports.
410 (:pep:`0328`)
411
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000412
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000413Changes Already Present In Python 2.6
414=====================================
415
Guido van Rossum715287f2008-12-02 22:34:15 +0000416Since many users presumably make the jump straight from Python 2.5 to
417Python 3.0, this section reminds the reader of new features that were
418originally designed for Python 3.0 but that were back-ported to Python
4192.6. The corresponding sections in :ref:`whats-new-in-2.6` should be
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000420consulted for longer descriptions.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000421
Guido van Rossum715287f2008-12-02 22:34:15 +0000422* :ref:`pep-0343`. The :keyword:`with` statement is now a standard
423 feature and no longer needs to be imported from the ``__future__``.
424 Also check out :ref:`new-26-context-managers` and
425 :ref:`new-module-contextlib`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000426
Guido van Rossum715287f2008-12-02 22:34:15 +0000427* :ref:`pep-0366`. This enhances the usefulness of the :option:`-m`
428 option when the referenced module lives in a package.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000429
Guido van Rossum715287f2008-12-02 22:34:15 +0000430* :ref:`pep-0370`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000431
Guido van Rossum715287f2008-12-02 22:34:15 +0000432* :ref:`pep-0371`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000433
Guido van Rossum715287f2008-12-02 22:34:15 +0000434* :ref:`pep-3101`. Note: the 2.6 description mentions the
435 :meth:`format` method for both 8-bit and Unicode strings. In 3.0,
436 only the :class:`str` type (text strings with Unicode support)
437 supports this method; the :class:`bytes` type does not. The plan is
438 to eventually make this the only API for string formatting, and to
439 start deprecating the ``%`` operator in Python 3.1.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000440
Guido van Rossum715287f2008-12-02 22:34:15 +0000441* :ref:`pep-3105`. This is now a standard feature and no longer needs
442 to be imported from :mod:`__future__`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000443
Guido van Rossum715287f2008-12-02 22:34:15 +0000444* :ref:`pep-3110`. The :keyword:`except` *exc* :keyword:`as` *var*
445 syntax is now standard and :keyword:`except` *exc*, *var* is no
446 longer supported. (Of course, the :keyword:`as` *var* part is still
447 optional.)
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000448
Guido van Rossum715287f2008-12-02 22:34:15 +0000449* :ref:`pep-3112`. The ``b"..."`` string literal notation (and its
450 variants like ``b'...'``, ``b"""..."""``, and ``br"..."``) now
Guido van Rossum38287682008-12-03 02:03:19 +0000451 produces a literal of type :class:`bytes`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000452
Guido van Rossum715287f2008-12-02 22:34:15 +0000453* :ref:`pep-3116`. The :mod:`io` module is now the standard way of
454 doing file I/O, and the initial values of :data:`sys.stdin`,
455 :data:`sys.stdout` and :data:`sys.stderr` are now instances of
456 :class:`io.TextIOBase`. The builtin :func:`open` function is now an
457 alias for :func:`io.open` and has additional keyword arguments
458 *encoding*, *errors*, *newline* and *closefd*. Also note that an
459 invalid *mode* argument now raises :exc:`ValueError`, not
Guido van Rossum38287682008-12-03 02:03:19 +0000460 :exc:`IOError`. The binary file object underlying a text file
461 object can be accessed as :attr:`f.buffer` (but beware that the
462 text object maintains a buffer of itself in order to speed up
463 the encoding and decoding operations).
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000464
Guido van Rossum715287f2008-12-02 22:34:15 +0000465* :ref:`pep-3118`. The old builtin :func:`buffer` is now really gone;
466 the new builtin :func:`memoryview` provides (mostly) similar
467 functionality.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000468
Guido van Rossum715287f2008-12-02 22:34:15 +0000469* :ref:`pep-3119`. The :mod:`abc` module and the ABCs defined in the
Guido van Rossum38287682008-12-03 02:03:19 +0000470 :mod:`collections` module plays a somewhat more prominent role in
Guido van Rossum715287f2008-12-02 22:34:15 +0000471 the language now, and builtin collection types like :class:`dict`
472 and :class:`list` conform to the :class:`collections.MutableMapping`
473 and :class:`collections.MutableSequence` ABC, respectively.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000474
Guido van Rossum715287f2008-12-02 22:34:15 +0000475* :ref:`pep-3127`. As mentioned above, the new octal literal
476 notation is the only one supported, and binary literals have been
477 added.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000478
Guido van Rossum38287682008-12-03 02:03:19 +0000479* :ref:`pep-3129`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000480
Guido van Rossum715287f2008-12-02 22:34:15 +0000481* :ref:`pep-3141`. The :mod:`numbers` module is another new use of
482 ABCs, defining Python's "numeric tower". Also note the new
Guido van Rossum38287682008-12-03 02:03:19 +0000483 :mod:`fractions` module which implements :class:`numbers.Rational`.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000484
485
486Library Changes
487===============
488
Guido van Rossumc46ee542008-12-02 23:46:46 +0000489Due to time constraints, this document does not exhaustively cover the
490very extensive changes to the standard library. :pep:`3108` is the
491reference for the major changes to the library. Here's a capsule
492review:
Guido van Rossum56076da2008-12-02 22:58:36 +0000493
Guido van Rossumc46ee542008-12-02 23:46:46 +0000494* Many old modules were removed. Some, like :mod:`gopherlib` (no
495 longer used) and :mod:`md5` (replaced by :mod:`hashlib`), were
496 already deprecated by :pep:`0004`. Others were removed as a result
497 of the removal of support for various platforms such as Irix, BeOS
498 and Mac OS 9 (see :pep:`0011`). Some modules were also selected for
499 removal in Python 3.0 due to lack of use or because a better
500 replacement exists. See :pep:`3108` for an exhaustive list.
Guido van Rossum5b78dd92008-12-02 17:31:14 +0000501
Guido van Rossumc46ee542008-12-02 23:46:46 +0000502* The :mod:`bsddb3` package was removed because its presence in the
503 core standard library has proved over time to be a particular burden
504 for the core developers due to testing instability and Berlekey DB's
505 release schedule. However, the package is alive and well,
506 externally maintained at http://www.jcea.es/programacion/pybsddb.htm.
507
508* Some modules were renamed because their old name flaunted
509 :pep:`0008`, or for various other reasons:
510
511 ======================= =======================
512 Old Name New Name
513 ======================= =======================
514 _winreg winreg
515 ConfigParser configparser
516 copy_reg copyreg
517 Queue queue
518 SocketServer socketserver
519 markupbase _markupbase
520 repr reprlib
521 test.test_support test.support
522 ======================= =======================
523
524* A common pattern in Python 2.x is to have one version of a module
525 implemented in pure Python, with an optional accelerated version
526 implemented as a C extension; for example, :mod:`pickle` and
527 :mod:`cPickle`. This places the burden of importing the accelerated
528 version and falling back on the pure Python version on each user of
529 these modules. In Python 3.0, the accelerated versions are
530 considered implementation details of the pure Python versions.
531 Users should always import the standard version, which attempts to
532 import the accelerated version and falls back to the pure Python
533 version. The :mod:`pickle` module received this treatment. The
534 :mod:`profile` module is on the list for 3.1. The :mod:`StringIO`
535 module has been turned into a class in the :mod:`io` module.
536
537* Some related modules have been grouped into packages, and usually
538 the submodule names have been simplified. The resulting new
539 packages are:
540
541 * :mod:`dbm` (:mod:`anydbm`, :mod:`dbhash`, :mod:`dbm`,
542 :mod:`dumbdbm`, :mod:`gdbm`, :mod:`whichdb`).
543
544 * :mod:`html` (:mod:`HTMLParser`, :mod:`htmlentitydefs`).
545
546 * :mod:`http` (:mod:`httplib`, :mod:`BaseHTTPServer`,
547 :mod:`CGIHTTPServer`, :mod:`SimpleHTTPServer`, :mod:`Cookie`,
548 :mod:`cookielib`).
549
550 * :mod:`tkinter` (all :mod:`Tkinter`-related modules except
551 :mod:`turtle`). The target audience of :mod:`turtle` doesn't
552 really care about :mod:`tkinter`. Also note that as of Python
553 2.6, the functionality of :mod:`turtle` has been greatly enhanced.
554
555 * :mod:`urllib` (:mod:`urllib`, :mod:`urllib`2, :mod:`urlparse`,
556 :mod:`robotparse`).
557
558 * :mod:`xmlrpc` (:mod:`xmlrpclib`, :mod:`DocXMLRPCServer`,
559 :mod:`SimpleXMLRPCServer`).
560
561Some other library changes (not covered by :pep:`3108`):
Guido van Rossum715287f2008-12-02 22:34:15 +0000562
563* Killed :mod:`sets`. Use the builtin :func:`set` function.
564
Guido van Rossumc46ee542008-12-02 23:46:46 +0000565* Cleanup of the :mod:`sys` module: removed :func:`sys.exitfunc`,
566 :func:`sys.exc_clear`, :data:`sys.exc_type`, :data:`sys.exc_value`,
567 :data:`sys.exc_traceback`. (Note that :data:`sys.last_type`
Guido van Rossum715287f2008-12-02 22:34:15 +0000568 etc. remain.)
569
Guido van Rossumc46ee542008-12-02 23:46:46 +0000570* Cleanup of the :class:`array.array` type: the :meth:`read` and
571 :meth:`write` methods are gone; use :meth:`fromfile` and
572 :meth:`tofile` instead.
Guido van Rossum715287f2008-12-02 22:34:15 +0000573
Guido van Rossumc46ee542008-12-02 23:46:46 +0000574* Cleanup of the :mod:`operator` module: removed
575 :func:`sequenceIncludes` and :func:`isCallable`.
Guido van Rossum715287f2008-12-02 22:34:15 +0000576
Guido van Rossumc46ee542008-12-02 23:46:46 +0000577* Cleanup of the :mod:`thread` module: :func:`acquire_lock` and
578 :func:`release_lock` are gone; use :func:`acquire` and
579 :func:`release` instead.
Guido van Rossum715287f2008-12-02 22:34:15 +0000580
Guido van Rossumc46ee542008-12-02 23:46:46 +0000581* Cleanup of the :mod:`random` module: removed the :func:`jumpahead` API.
Guido van Rossum715287f2008-12-02 22:34:15 +0000582
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000583
Guido van Rossum715287f2008-12-02 22:34:15 +0000584:pep:`3101`: A New Approach To String Formatting
585================================================
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000586
Guido van Rossum4a98a2a2008-11-21 18:35:43 +0000587* A new system for built-in string formatting operations replaces the
588 ``%`` string formatting operator. (However, the ``%`` operator is
589 still supported; it will be deprecated in Python 3.1 and removed
Guido van Rossum38287682008-12-03 02:03:19 +0000590 from the language at some later time.) Read :pep:`3101` for the full
591 scoop.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000592
593
Guido van Rossum715287f2008-12-02 22:34:15 +0000594:pep:`3106`: Revamping dict :meth:`dict.keys`, :meth:`dict.items` and :meth:`dict.values`
595=========================================================================================
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000596
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000597.. XXX expand this (but note that the "pitfalls" section currently has
598.. XXX more detail :-)
Georg Brandl396ef802008-02-02 10:30:18 +0000599
Georg Brandlec17d202008-02-02 10:44:37 +0000600* The :meth:`dict.iterkeys`, :meth:`dict.itervalues` and :meth:`dict.iteritems`
601 methods have been removed.
Georg Brandl396ef802008-02-02 10:30:18 +0000602
Georg Brandlec17d202008-02-02 10:44:37 +0000603* :meth:`dict.keys`, :meth:`dict.values` and :meth:`dict.items` return objects
Guido van Rossum4a98a2a2008-11-21 18:35:43 +0000604 with set behavior that reference the underlying dict; these are often
605 referred to as *dictionary views*.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000606
607
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000608Exception Stuff
609===============
610
Guido van Rossum715287f2008-12-02 22:34:15 +0000611* :pep:`0352`: All exceptions must be derived (directly or indirectly)
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000612 from :exc:`BaseException`. This is the root of the exception
613 hierarchy. Most exceptions should actually be derived from
614 :exc:`Exception`. This is not a new recommendation, but the
615 *requirement* to inherit from :exc:`BaseException` is new. (Python
616 2.6 still allowed classic classes to be raised, and placed no
617 restriction on what you can catch.)
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000618
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000619* :exc:`StandardError` was removed (in 2.6, actually).
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000620
Georg Brandlec17d202008-02-02 10:44:37 +0000621* Dropping sequence behavior (slicing!) and :attr:`message` attribute of
Georg Brandl5a165582007-08-31 06:15:01 +0000622 exception instances.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000623
Guido van Rossum715287f2008-12-02 22:34:15 +0000624* :pep:`3109`: Raising exceptions. You must now use ``raise Exception(args)``
Georg Brandlec17d202008-02-02 10:44:37 +0000625 instead of ``raise Exception, args``.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000626
Guido van Rossum715287f2008-12-02 22:34:15 +0000627* :pep:`3110`: Catching exceptions. You must now use ``except SomeException as
Georg Brandle06de8b2008-05-05 21:42:51 +0000628 identifier:`` instead of ``except Exception, identifier:``
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000629
Guido van Rossum715287f2008-12-02 22:34:15 +0000630* :pep:`3134`: Exception chaining. XXX
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000631
Georg Brandlec17d202008-02-02 10:44:37 +0000632* A few exception messages are improved when Windows fails to load an extension
633 module. For example, ``error code 193`` is now ``%1 is not a valid Win32
634 application``. Strings now deal with non-English locales.
Georg Brandl396ef802008-02-02 10:30:18 +0000635
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000636
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000637New Class And Metaclass Stuff
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000638=============================
639
Guido van Rossum38287682008-12-03 02:03:19 +0000640XXX Move to new syntax section???
641
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000642* Classic classes are gone.
643
Guido van Rossum38287682008-12-03 02:03:19 +0000644* :pep:`3115`: New Metaclass Syntax. Instead of::
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000645
Guido van Rossum38287682008-12-03 02:03:19 +0000646 class C:
647 __metaclass__ = M
648 ...
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000649
Guido van Rossum38287682008-12-03 02:03:19 +0000650 you now use::
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000651
Guido van Rossum38287682008-12-03 02:03:19 +0000652 class C(metaclass=M):
653 ...
654
655 The module-global :data:`__metaclass__` variable is no longer supported.
656 (It was a crutch to make it easier to default to new-style classes
657 without deriving every class from :class:`object`.)
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000658
659
Georg Brandl116aa622007-08-15 14:28:22 +0000660Other Language Changes
661======================
662
Guido van Rossum715287f2008-12-02 22:34:15 +0000663* Moved :func:`intern` to :func:`sys.intern`.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000664
Georg Brandl396ef802008-02-02 10:30:18 +0000665* ``!=`` now returns the opposite of ``==``, unless ``==`` returns
666 ``NotImplemented``.
667
Guido van Rossum715287f2008-12-02 22:34:15 +0000668* The concept of "unbound methods" was removed from the language.
669 When referencing a method as a class attribute, you now get a plain
670 function object.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000671
Guido van Rossum715287f2008-12-02 22:34:15 +0000672* :meth:`__getslice__`, :meth:`__setslice__` and :meth:`__delslice__`
673 were killed. The syntax ``a[i:j]`` now translates to
674 ``a.__getitem__(slice(i, j))`` (or :meth:`__setitem__` or
675 :meth:`__delitem__`, when used as an assignment or deletion target,
676 respectively).
Georg Brandl396ef802008-02-02 10:30:18 +0000677
Guido van Rossum715287f2008-12-02 22:34:15 +0000678* :pep:`3111`: :func:`raw_input` renamed to :func:`input`. That is,
679 the new :func:`input` function reads a line from :data:`sys.stdin`
680 and returns it with the trailing newline stripped. It raises
681 :exc:`EOFError` if the input is terminated prematurely. To get the
682 old behavior of :func:`input`, use ``eval(input())``.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000683
Guido van Rossum715287f2008-12-02 22:34:15 +0000684* :pep:`3114`: ``.next()`` renamed to :meth:`__next__`, new builtin
685 :func:`next` to call the :meth:`__next__` method on an object.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000686
Guido van Rossum715287f2008-12-02 22:34:15 +0000687* :pep:`3135`: New :func:`super`. You can now invoke :func:`super`
688 without arguments and the right class and instance will
689 automatically be chosen. With arguments, its behavior is unchanged.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000690
Georg Brandlec17d202008-02-02 10:44:37 +0000691* :func:`zip`, :func:`map` and :func:`filter` return iterators.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000692
Georg Brandlec17d202008-02-02 10:44:37 +0000693* :data:`string.letters` and its friends (:data:`string.lowercase` and
Guido van Rossum715287f2008-12-02 22:34:15 +0000694 :data:`string.uppercase`) are gone. Use
695 :data:`string.ascii_letters` etc. instead. (The reason for the
696 removal is that :data:string.letters` and friends had
697 locale-specific behavior, which is a bad idea for such
698 attractively-named global "constants".)
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000699
Guido van Rossum715287f2008-12-02 22:34:15 +0000700* Removed: :func:`apply`. Instead of ``apply(f, args)`` use
701 ``f(*args)``.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000702
Guido van Rossum715287f2008-12-02 22:34:15 +0000703* Removed :func:`callable`. Instead of ``callable(f)`` you can use
704 ``hasattr(f, '__call__')``. The :func:`operator.isCallable` function
705 is also gone.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000706
Guido van Rossum715287f2008-12-02 22:34:15 +0000707* Removed :func:`coerce`. This function no longer serves a purpose
708 now that classic classes are gone.
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000709
Guido van Rossum715287f2008-12-02 22:34:15 +0000710* Removed :func:`execfile`. Instead of ``execfile(fn)`` use
711 ``exec(open(fn).read())``.
Georg Brandl396ef802008-02-02 10:30:18 +0000712
Guido van Rossum715287f2008-12-02 22:34:15 +0000713* Removed :class:`file`. Use :func:`open`.
Georg Brandl396ef802008-02-02 10:30:18 +0000714
Guido van Rossum715287f2008-12-02 22:34:15 +0000715* Removed :func:`reduce`. Use :func:`functools.reduce` if you really
716 need it; however, 99 percent of the time an explicit :keyword:`for`
717 loop is more readable.
Georg Brandl396ef802008-02-02 10:30:18 +0000718
Guido van Rossum715287f2008-12-02 22:34:15 +0000719* Removed :func:`reload`. Use :func:`imp.reload`.
720
721* Removed. :meth:`dict.has_key` -- use the :keyword:`in` operator
722 instead.
723
724* The :meth:`__oct__` and :meth:`__hex__` special methods are removed
725 -- :func:`oct` and :func:`hex` use :meth:`__index__` now to convert
726 the argument to an integer.
727
728* Removed support for :attr:`__members__` and :attr:`__methods__`.
729
730* Renamed the boolean conversion C-level slot and method:
731 ``nb_nonzero`` is now ``nb_bool`` and :meth:`__nonzero__` is now
732 :meth:`__bool__`.
733
734* Renamed module :mod:`__builtin__` to :mod:`builtins` (removing the
735 underscores, adding an 's'). The :data:`__builtins__` variable
736 found in most global namespaces is unchanged. To modify a builtin,
737 you should use :mod:`builtins`, not :data:`__builtins__`!
738
739* Renamed function attributes :attr:`func_whatever` to
740 :attr:`__whatever__`. XXX list every single one.
741
742* Removed :exc:`StandardError`.
743
744* Removed METH_OLDARGS and WITH_CYCLE_GC. XXX more.
Georg Brandl116aa622007-08-15 14:28:22 +0000745
Georg Brandl5a165582007-08-31 06:15:01 +0000746.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000747
748
749Optimizations
750-------------
751
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000752The net result of the 3.0 generalizations is that Python 3.0 runs the
Guido van Rossum715287f2008-12-02 22:34:15 +0000753pystone benchmark around 10% slower than Python 2.5. Most likely the
754biggest cause is the removal of special-casing for small integers.
755There's room for improvement, but it will happen after 3.0 is
756released!
Georg Brandl116aa622007-08-15 14:28:22 +0000757
Georg Brandl5a165582007-08-31 06:15:01 +0000758.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000759
760
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000761New, Improved, And Deprecated Modules
Georg Brandl116aa622007-08-15 14:28:22 +0000762=====================================
763
Georg Brandlec17d202008-02-02 10:44:37 +0000764As usual, Python's standard library received a number of enhancements and bug
765fixes. Here's a partial list of the most notable changes, sorted alphabetically
766by module name. Consult the :file:`Misc/NEWS` file in the source tree for a more
767complete list of changes, or look through the Subversion logs for all the
768details.
Georg Brandl116aa622007-08-15 14:28:22 +0000769
Georg Brandlec17d202008-02-02 10:44:37 +0000770* The :mod:`cPickle` module is gone. Use :mod:`pickle` instead. Eventually
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000771 we'll have a transparent accelerator module.
Georg Brandl116aa622007-08-15 14:28:22 +0000772
Georg Brandlec17d202008-02-02 10:44:37 +0000773* The :mod:`imageop` module is gone.
Georg Brandl396ef802008-02-02 10:30:18 +0000774
Georg Brandlec17d202008-02-02 10:44:37 +0000775* The :mod:`audiodev`, :mod:`Bastion`, :mod:`bsddb185`, :mod:`exceptions`,
776 :mod:`linuxaudiodev`, :mod:`md5`, :mod:`MimeWriter`, :mod:`mimify`,
777 :mod:`popen2`, :mod:`rexec`, :mod:`sets`, :mod:`sha`, :mod:`stringold`,
778 :mod:`strop`, :mod:`sunaudiodev`, :mod:`timing`, and :mod:`xmllib` modules are
779 gone.
Georg Brandl396ef802008-02-02 10:30:18 +0000780
Gregory P. Smith7b9a2222008-09-04 05:07:03 +0000781* The :mod:`bsddb` module is gone. It is being maintained externally
782 with its own release schedule better mirroring that of BerkeleyDB.
783 See http://www.jcea.es/programacion/pybsddb.htm.
784
Georg Brandlec17d202008-02-02 10:44:37 +0000785* The :mod:`new` module is gone.
Georg Brandl396ef802008-02-02 10:30:18 +0000786
Georg Brandlec17d202008-02-02 10:44:37 +0000787* The functions :func:`os.tmpnam`, :func:`os.tempnam` and :func:`os.tmpfile`
788 have been removed in favor of the :mod:`tempfile` module.
Georg Brandl396ef802008-02-02 10:30:18 +0000789
Trent Nelson428de652008-03-18 22:41:35 +0000790* The :mod:`tokenize` module has been changed to work with bytes. The main
791 entry point is now :func:`tokenize.tokenize`, instead of generate_tokens.
792
Georg Brandl5a165582007-08-31 06:15:01 +0000793.. ======================================================================
794.. whole new modules get described in subsections here
Georg Brandl116aa622007-08-15 14:28:22 +0000795
Georg Brandl5a165582007-08-31 06:15:01 +0000796.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000797
798
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000799Build And C API Changes
Georg Brandl116aa622007-08-15 14:28:22 +0000800=======================
801
802Changes to Python's build process and to the C API include:
803
Guido van Rossum715287f2008-12-02 22:34:15 +0000804* :pep:`3118`: New Buffer API. XXX
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000805
Guido van Rossum715287f2008-12-02 22:34:15 +0000806* :pep:`3121`: Extension Module Initialization & Finalization. XXX
Guido van Rossumb197f3c2007-08-31 00:37:00 +0000807
Guido van Rossum715287f2008-12-02 22:34:15 +0000808* :pep:`3123`: Making :cmacro:`PyObject_HEAD` conform to standard C. XXX
Georg Brandl116aa622007-08-15 14:28:22 +0000809
Georg Brandl396ef802008-02-02 10:30:18 +0000810* No more C API support for restricted execution.
811
Georg Brandlec17d202008-02-02 10:44:37 +0000812* :cfunc:`PyNumber_Coerce`, :cfunc:`PyNumber_CoerceEx`, :cfunc:`PyMember_Get`,
813 and :cfunc:`PyMember_Set` C APIs are removed.
Georg Brandl396ef802008-02-02 10:30:18 +0000814
Georg Brandlec17d202008-02-02 10:44:37 +0000815* New C API :cfunc:`PyImport_ImportModuleNoBlock`, works like
816 :cfunc:`PyImport_ImportModule` but won't block on the import lock (returning
Georg Brandl396ef802008-02-02 10:30:18 +0000817 an error instead).
818
Georg Brandl5a165582007-08-31 06:15:01 +0000819.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000820
821
822Port-Specific Changes
823---------------------
824
Guido van Rossum715287f2008-12-02 22:34:15 +0000825XXX Platform-specific changes go here.
Georg Brandl116aa622007-08-15 14:28:22 +0000826
Guido van Rossum715287f2008-12-02 22:34:15 +0000827* XXX BeOS, RISCOS, Irix, Tru64 support
Georg Brandl396ef802008-02-02 10:30:18 +0000828
Georg Brandl5a165582007-08-31 06:15:01 +0000829.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000830
831
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000832Porting To Python 3.0
Georg Brandl116aa622007-08-15 14:28:22 +0000833=====================
834
Guido van Rossum56076da2008-12-02 22:58:36 +0000835For porting existing Python 2.5 or 2.6 source code to Python 3.0, the
836best strategy is the following:
Georg Brandl116aa622007-08-15 14:28:22 +0000837
Guido van Rossum56076da2008-12-02 22:58:36 +00008380. (Prerequisite:) Start with excellent test coverage.
Georg Brandl116aa622007-08-15 14:28:22 +0000839
Guido van Rossum56076da2008-12-02 22:58:36 +00008401. Port to Python 2.6. This should be no more work than the average
841 port from Python 2.x to Python 2.(x+1). Make sure all your tests
842 pass.
Christian Heimesf78b1c62007-12-02 16:52:32 +0000843
Guido van Rossum56076da2008-12-02 22:58:36 +00008442. (Still using 2.6:) Turn on the :option:`-3` command line switch.
845 This enables warnings about features that will be removed (or
846 change) in 3.0. Run your test suite again, and fix code that you
847 get warnings about until there are no warnings left, and all your
848 tests still pass.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000849
Guido van Rossum56076da2008-12-02 22:58:36 +00008503. Run the ``2to3`` source-to-source translator over your source code
851 tree. (See :ref:`2to3-reference` for more on this tool.) Run the
852 result of the translation under Python 3.0. Manually fix up any
853 remaining issues, fixing problems until all tests pass again.
854
855It is not recommended to try to write source code that runs unchanged
856under both Python 2.6 and 3.0; you'd have to use a very contorted
857coding style, e.g. avoiding :keyword:`print` statements, metaclasses,
858and much more. If you are maintaining a library that needs to support
859both Python 2.6 and Python 3.0, the best approach is to modify step 3
860above by editing the 2.6 version of the source code and running the
861``2to3`` translator again, rather than editing the 3.0 version of the
862source code.
863
864For porting C extensions to Python 3.0, please see :ref:`cporting-howto`.
Guido van Rossumeb3d8d42008-12-02 00:56:25 +0000865
Georg Brandl5a165582007-08-31 06:15:01 +0000866.. ======================================================================